简体   繁体   中英

why is concurrent_queue non-blocking?

In the concurrency runtime introduced in VS2010, there is a concurrent_queue class. It has a non blocking try_pop() function.
Similar in Intel Thread Building Blocks (TBB), the blocking pop() call was removed when going from version 2.1 to 2.2.

I wonder what the problem is with a blocking call. Why was it removed from TBB? And why is there no blocking concurrent_queue?

I'm in a situation where I need a blocking concurrent queue, and I don't want a busy wait. Apart from writing a queue myself, is there another possibility in the concurrency runtime?

From a comment from Arch Robison , and it doesn't get much more "horse's mouth" than that (a) :


PPL's concurrent_queue has no blocking pop, hence neither does tbb::strict_ppl::concurrent_queue . The blocking pop is available in tbb::concurrent_bounded_queue .

The design argument for omitting blocking pop is that in many cases, the synchronization for blocking is provided outside of the queue, in which case the implementation of blocking inside the queue becomes unnecessary overhead.

On the other hand, the blocking pop of the old tbb::concurrent_queue was popular among users who did not have outside synchronization.

So we split the functionality. Use cases that do not need blocking or boundedness can use the new tbb::concurrent_queue , and use cases that do need it can use tbb::concurrent_bounded_queue .


(a) Arch is the architect of Threading Building Blocks.

If you need a blocking pop without a busy wait, you need a method of signaling. This implies synchronization between pusher and poper and the queue is no longer without (expensive) synchronization primitives. You basically get a normal synchronized queue with a condition variable being used to notify poppers of pushes, which is not in the spirity of the concurrent_* collections.

The question was if there was another option in the Concurrency Runtime that provides blocking queue functionality because concurrent_queue does not and there is one in VS2010.

Arch's comment is of course completely correct, blocking queues and unblocking queues are separate use cases and this is why they are different in VS2010 and in TBB.

In VS2010 you can use the template class unbounded_buffer located in , the appropriate methods are called enqueue and dequeue.

-Rick

There is no situation, from the queue's standpoint, that it should need to block for an insert or remove. The fact that you may need to block and wait for an insert is immaterial.

You can achieve the functionality you desire by using a condition variable, or a counting semaphore, or something along those lines (whatever your specific API provides). Your trouble isn't with blocking/non-blocking; it sounds like a classic producer-consumer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM