简体   繁体   English

共享存储中的Intel TBB parallel_queue?

[英]Intel TBB concurrent_queue in shared memory?

I am trying to setup a queue in shared memory such that processes P1, P2 etc can push messages to the same queue, which are to be consumed by another process C1. 我试图在共享内存中设置一个队列,以便进程P1,P2等可以将消息推送到同一队列,这些消息将由另一个进程C1使用。

I used boost::interprocess::managed_shared_memory + boost::lockfree::queue (fixed size at compile time) and it worked. 我使用了boost :: interprocess :: managed_shared_memory + boost :: lockfree :: queue(在编译时固定大小),它起作用了。 No special allocator required. 不需要特殊的分配器。

When I turned to boost::interprocess::managed_shared_memory + tbb::concurrent_bounded_queue (I like the blocking push and pop mechanism there), I couldn't get the code compiled. 当我转向boost :: interprocess :: managed_shared_memory + tbb :: concurrent_bounded_queue(我喜欢那里的阻塞推和弹出机制)时,我无法编译代码。

namespace bip = boost::interprocess;

typedef bip::allocator<DataType, bip::managed_shared_memory::segment_manager> ShmAlloc;
typedef tbb::concurrent_bounded_queue<DataType, ShmAlloc> BufferQueue;

// declare memory
bip::managed_shared_memory segment(bip::create_only, "MySharedMemory", 65536);

// initialize allocator
const ShmAlloc alloc_inst(segment.get_segment_manager());

// construct the queue in shared memory
BufferQueue *queue = segment.construct<BufferQueue>("data_queue")(alloc_inst);

With clang++ 6.0 in Mac OS, boost 1.56, tbb 4.3, I got the following error when compiling: 在Mac OS中使用clang ++ 6.0,boost 1.56,tbb 4.3时,在编译时出现以下错误:

In file included from tbbproducer.cpp:5:
/opt/homebrew/include/tbb/concurrent_queue.h:263:13: error: reinterpret_cast from 'pointer' (aka 'offset_ptr<char, long, unsigned long, 0UL>')
      to 'void *' is not allowed
                void *b = reinterpret_cast<void*>(my_allocator.allocate( n ));

Question 1: How could I fixed this? 问题1:我该如何解决? Can tbb::concurrent_queue to be used in memory? 可以在内存中使用tbb :: concurrent_queue吗?

Question 2: If (1) is impossible, are there other queue supporting blocking push and pop that I can use in shared memory? 问题2:如果(1)是不可能的,是否还有其他队列可以阻止我在共享内存中使用来阻止推入和弹出?

Thanks! 谢谢!

Regardless to the reason behind the compilation issue, tbb::concurrent_queue is not ready to work in the interprocess shared memory along with tbb::concurrent_bounded_queue , tbb::concurrent_vector , and tbb::concurrent_hash_map because these containers allocate not all the memory with user-provided allocator instance (at least, up to TBB 4.3). 不管编译问题背后的原因是什么, tbb::concurrent_queue都不准备与tbb::concurrent_bounded_queuetbb::concurrent_vectortbb::concurrent_hash_map一起在进程间共享内存中工作,因为这些容器不会为用户分配所有内存提供的分配器实例(至少为TBB 4.3)。

There are two kinds of container which might work as expected because they allocate all the memory through user-provided allocator instance: tbb::concurrent_priority_queue and tbb::concurrent_unordered_* family of containers. 有两种容器可能会按预期工作,因为它们通过用户提供的分配器实例分配所有内存: tbb::concurrent_priority_queuetbb::concurrent_unordered_*系列容器。 You might want to try the first one instead of tbb::concurrent_queue . 您可能要尝试第一个而不是tbb::concurrent_queue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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