concurrent_queue
The template class concurrent_queue<T> implements a concurrent queue with values of type T. Multiple threads may simultaneously push and pop elements from the queue.
In a single-threaded program, a queue is a first-in first-out structure. But if multiple threads are pushing and popping concurrently, the definition of first is uncertain. The only guarantee of ordering offered by concurrent_queue is that if a thread pushes multiple values, and another thread pops those same values, they will be popped in the same order that they were pushed.
Pushing is provided by the push method. There are blocking and nonblocking flavors of pop:
-
pop_if_present This method is nonblocking: it attempts to pop a value, and if it cannot because the queue is empty, it returns anyway.
-
pop This method blocks until it pops a value. If a thread must wait for an item to become available and it has nothing else to do, it should use
pop(item)and notwhile(!pop_if_present(item)) continue; becausepopuses processor resources more efficiently than the loop.
Unlike most STL containers, concurrent_queue::size_type is a signed integral type, not unsigned. This is because concurrent_queue::size() is defined as the number of push operations started minus the number of pop operations started. If pops out-number pushes, size() becomes negative. For example, if a concurrent_queue is empty and there are n pending pop operations, size() returns –n. This provides an easy way for producers to know how many ...