TBB concurrent_bounded queue in C++ and synchronization
I am using TBB concurrent_bounded_queue, as this class allows us to use pop which is blocked if no elements are available. What is the default size of 开发者_运维问答the queue? I also read in a book instead of using concurrent_bounded_queue use parallel_while or pipeline, how these will help us in place of concurrent_bounded queue? can any one pls give an example how paralle_while or pipeline can be used to share data between two threads with synchronization?
Thanks!
The default size is of course zero, i.e. no elements in the queue. But you probably want to know the default capacity (i.e. the bound). The TBB Reference manual says that
A concurrent_bounded_queue <...> adds the ability to specify a capacity. The default capacity makes the queue practically unbounded.
And source code inspection (src/tbb/concurrent_queue.cpp) gives the following formula for default initialization of capacity:
my_capacity = size_t(-1)/(item_size>1 ? item_size : 2);
As for pipeline
or parallel_while
(note the latter is deprecated), those are algorithms, not containers, and so cannot be used to share data between two threads. But in some cases, use of pipeline
can replace a thread-based design. For example, a simple producer-consumer scenario may be replaced with a two-stage pipeline in which the first stage produces a piece of data and passes it to the second stage to process (consume).
精彩评论