C++ 中的 TBB并发有界队列和同步
我正在使用 TBB concurrent_bounded_queue,因为此类允许我们使用 pop,如果没有可用元素,则 pop 会被阻止。队列的默认大小是多少?我还在书中读到,不使用concurrent_bounded_queue而是使用parallel_while或pipeline,这些将如何帮助我们代替concurrent_bounded队列?任何人都可以举一个例子,如何使用 paralle_while 或 pipeline 在两个线程之间同步共享数据?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认大小当然为零,即队列中没有元素。但您可能想知道默认容量(即界限)。 TBB 参考手册 说
源代码检查(src/tbb/concurrent_queue.cpp)给出了容量默认初始化的以下公式:
对于
pipeline
或parallel_while
(注意后者已被弃用),这些是算法,而不是容器,因此不能用于在两个线程之间共享数据。但在某些情况下,使用管道可以取代基于线程的设计。例如,一个简单的生产者-消费者场景可以替换为两级管道,其中第一阶段产生一段数据并将其传递到第二阶段进行处理(消费)。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
And source code inspection (src/tbb/concurrent_queue.cpp) gives the following formula for default initialization of capacity:
As for
pipeline
orparallel_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 ofpipeline
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).