Reputation: 433
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.
I used boost::interprocess::managed_shared_memory + boost::lockfree::queue (fixed size at compile time) and it worked. 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.
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:
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? Can tbb::concurrent_queue to be used in memory?
Question 2: If (1) is impossible, are there other queue supporting blocking push and pop that I can use in shared memory?
Thanks!
Upvotes: 1
Views: 936
Reputation: 6537
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).
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. You might want to try the first one instead of tbb::concurrent_queue
.
Upvotes: 1