【问题标题】:Intel TBB concurrent_queue in shared memory?共享内存中的英特尔 TBB concurrent_queue?
【发布时间】: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) 不可能,是否还有其他支持阻塞推送和弹出的队列可以在共享内存中使用?

谢谢!

【问题讨论】:

    标签: c++ boost tbb


    【解决方案1】:

    不管编译问题背后的原因是什么,tbb::concurrent_queue 还没有准备好与tbb::concurrent_bounded_queuetbb::concurrent_vectortbb::concurrent_hash_map 一起在进程间共享内存中工作,因为这些容器并没有为用户分配所有内存-提供分配器实例(至少,直到 TBB 4.3)。

    有两种容器可以按预期工作,因为它们通过用户提供的分配器实例分配所有内存:tbb::concurrent_priority_queuetbb::concurrent_unordered_* 系列容器。您可能想尝试第一个而不是 tbb::concurrent_queue

    【讨论】:

    • 酷。使用 tbb::concurrent_priority_queue 使其编译。然而程序因段错误而失败,而 boost::lockfree::queue 工作没有问题。将进行更多调查并报告。
    猜你喜欢
    • 2023-03-25
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多