1*67c11716SAlexey Oralov.. _Concurrent_Queue_Classes: 2*67c11716SAlexey Oralov 3*67c11716SAlexey OralovConcurrent Queue Classes 4*67c11716SAlexey Oralov======================== 5*67c11716SAlexey Oralov 6*67c11716SAlexey Oralov 7*67c11716SAlexey OralovTemplate class ``concurrent_queue<T,Alloc>`` implements a concurrent 8*67c11716SAlexey Oralovqueue with values of type ``T``. Multiple threads may simultaneously 9*67c11716SAlexey Oralovpush and pop elements from the queue. The queue is unbounded and has no 10*67c11716SAlexey Oralovblocking operations. The fundamental operations on it are ``push`` and 11*67c11716SAlexey Oralov``try_pop``. The ``push`` operation works just like ``push`` for a 12*67c11716SAlexey Oralovstd::queue. The operation ``try_pop`` pops an item if it is available. 13*67c11716SAlexey OralovThe check and popping have to be done in a single operation for sake of 14*67c11716SAlexey Oralovthread safety. 15*67c11716SAlexey Oralov 16*67c11716SAlexey Oralov 17*67c11716SAlexey OralovFor example, consider the following serial code: 18*67c11716SAlexey Oralov 19*67c11716SAlexey Oralov 20*67c11716SAlexey Oralov:: 21*67c11716SAlexey Oralov 22*67c11716SAlexey Oralov 23*67c11716SAlexey Oralov extern std::queue<T> MySerialQueue; 24*67c11716SAlexey Oralov T item; 25*67c11716SAlexey Oralov if( !MySerialQueue.empty() ) { 26*67c11716SAlexey Oralov item = MySerialQueue.front(); 27*67c11716SAlexey Oralov MySerialQueue.pop_front(); 28*67c11716SAlexey Oralov ... process item... 29*67c11716SAlexey Oralov } 30*67c11716SAlexey Oralov 31*67c11716SAlexey Oralov 32*67c11716SAlexey OralovEven if each std::queue method were implemented in a thread-safe manner, 33*67c11716SAlexey Oralovthe composition of those methods as shown in the example would not be 34*67c11716SAlexey Oralovthread safe if there were other threads also popping from the same 35*67c11716SAlexey Oralovqueue. For example, ``MySerialQueue.empty()`` might return true just 36*67c11716SAlexey Oralovbefore another thread snatches the last item from ``MySerialQueue``. 37*67c11716SAlexey Oralov 38*67c11716SAlexey Oralov 39*67c11716SAlexey OralovThe equivalent thread-safe |full_name| code is: 40*67c11716SAlexey Oralov 41*67c11716SAlexey Oralov 42*67c11716SAlexey Oralov:: 43*67c11716SAlexey Oralov 44*67c11716SAlexey Oralov 45*67c11716SAlexey Oralov extern concurrent_queue<T> MyQueue; 46*67c11716SAlexey Oralov T item; 47*67c11716SAlexey Oralov if( MyQueue.try_pop(item) ) { 48*67c11716SAlexey Oralov ...process item... 49*67c11716SAlexey Oralov } 50*67c11716SAlexey Oralov 51*67c11716SAlexey Oralov 52*67c11716SAlexey OralovIn a single-threaded program, a queue is a first-in first-out structure. 53*67c11716SAlexey OralovBut if multiple threads are pushing and popping concurrently, the 54*67c11716SAlexey Oralovdefinition of "first" is uncertain. Use of ``concurrent_queue`` 55*67c11716SAlexey Oralovguarantees that if a thread pushes two values, and another thread pops 56*67c11716SAlexey Oralovthose two values, they will be popped in the same order that they were 57*67c11716SAlexey Oralovpushed. 58*67c11716SAlexey Oralov 59*67c11716SAlexey Oralov 60*67c11716SAlexey OralovTemplate class ``concurrent_queue`` is unbounded and has no methods that 61*67c11716SAlexey Oralovwait. It is up to the user to provide synchronization to avoid overflow, 62*67c11716SAlexey Oralovor to wait for the queue to become non-empty. Typically this is 63*67c11716SAlexey Oralovappropriate when the synchronization has to be done at a higher level. 64*67c11716SAlexey Oralov 65*67c11716SAlexey Oralov 66*67c11716SAlexey OralovTemplate class ``concurrent_bounded_queue<T,Alloc>`` is a variant that 67*67c11716SAlexey Oralovadds blocking operations and the ability to specify a capacity. The 68*67c11716SAlexey Oralovmethods of particular interest on it are: 69*67c11716SAlexey Oralov 70*67c11716SAlexey Oralov 71*67c11716SAlexey Oralov- ``pop(item)`` waits until it can succeed. 72*67c11716SAlexey Oralov 73*67c11716SAlexey Oralov 74*67c11716SAlexey Oralov- ``push(item)`` waits until it can succeed without exceeding the 75*67c11716SAlexey Oralov queue's capacity. 76*67c11716SAlexey Oralov 77*67c11716SAlexey Oralov 78*67c11716SAlexey Oralov- ``try_push(item)`` pushes ``item`` only if it would not exceed the 79*67c11716SAlexey Oralov queue's capacity. 80*67c11716SAlexey Oralov 81*67c11716SAlexey Oralov 82*67c11716SAlexey Oralov- size() returns a *signed* integer. 83*67c11716SAlexey Oralov 84*67c11716SAlexey Oralov 85*67c11716SAlexey OralovThe value of concurrent_queue::size() is defined as the number of push 86*67c11716SAlexey Oralovoperations started minus the number of pop operations started. If pops 87*67c11716SAlexey Oralovoutnumber pushes, ``size()`` becomes negative. For example, if a 88*67c11716SAlexey Oralov``concurrent_queue`` is empty, and there are ``n`` pending pop 89*67c11716SAlexey Oralovoperations, ``size()`` returns -\ ``n``. This provides an easy way for 90*67c11716SAlexey Oralovproducers to know how many consumers are waiting on the queue. Method 91*67c11716SAlexey Oralov``empty()`` is defined to be true if and only if ``size()`` is not 92*67c11716SAlexey Oralovpositive. 93*67c11716SAlexey Oralov 94*67c11716SAlexey Oralov 95*67c11716SAlexey OralovBy default, a ``concurrent_bounded_queue`` is unbounded. It may hold any 96*67c11716SAlexey Oralovnumber of values, until memory runs out. It can be bounded by setting 97*67c11716SAlexey Oralovthe queue capacity with method ``set_capacity``.Setting the capacity 98*67c11716SAlexey Oralovcauses ``push`` to block until there is room in the queue. Bounded 99*67c11716SAlexey Oralovqueues are slower than unbounded queues, so if there is a constraint 100*67c11716SAlexey Oralovelsewhere in your program that prevents the queue from becoming too 101*67c11716SAlexey Oralovlarge, it is better not to set the capacity. If you do not need the 102*67c11716SAlexey Oralovbounds or the blocking pop, consider using ``concurrent_queue`` instead. 103*67c11716SAlexey Oralov 104*67c11716SAlexey Oralov.. toctree:: 105*67c11716SAlexey Oralov :maxdepth: 4 106*67c11716SAlexey Oralov 107*67c11716SAlexey Oralov ../tbb_userguide/Iterating_Over_a_Concurrent_Queue_for_Debugging 108*67c11716SAlexey Oralov ../tbb_userguide/When_Not_to_Use_Queues