【发布时间】:2015-02-05 08:35:33
【问题描述】:
我正在尝试在共享内存中设置一个队列,以便进程 P1、P2 等可以将消息推送到同一个队列,这些消息将由另一个进程 C1 使用。
我使用了 boost::interprocess::managed_shared_memory + boost::lockfree::queue (编译时固定大小)并且它有效。不需要特殊的分配器。
当我转向 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);
在 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 ));
问题 1: 我怎么能解决这个问题? tbb::concurrent_queue 可以在内存中使用吗?
问题 2: 如果 (1) 不可能,是否还有其他支持阻塞推送和弹出的队列可以在共享内存中使用?
谢谢!
【问题讨论】: