max0x7ba / atomic_queue

C++ lockless queue.
MIT License
1.47k stars 176 forks source link

Could you add more apis such as Front() and PushFront()? #35

Closed hahasofia closed 2 years ago

hahasofia commented 2 years ago

Dear author, I have a requirement such as to inquire the first element of queue but not pop it. It's just like what Front() does in std::vector. Also I want to push one element in the front of queue. Do you think these two functions Front and PushFront can be added in? Thank you.

max0x7ba commented 2 years ago

Do you think these two functions Front and PushFront can be added in?

In a multi-threaded application front would return a reference to an element in the queue. By the time the element is examined, another thread may have poped this element, leaving you with an invalid reference. For this reason front is not an applicable use case for this non-blocking queue.

May be try_pop is what you need.

Alternatively, you may like to use a non-blocking queue which is based on hazard- or smart-pointers, or use a blocking queue, e.g. std::vector with std::mutex.