[英]Create boost::spsc queue in boost managed shared memory with a runtime size
Shared-memory IPC synchronization (lock-free) 共享内存 IPC 同步(无锁)
My use case aligns very closely with what has been described in the above question.我的用例与上述问题中的描述非常接近。 But I wanted to go a step further in creating the spsc queue dynamically with a user defined runtime size.但我想进一步使用用户定义的运行时大小动态创建 spsc 队列。 I tried implementing it with the following code:我尝试使用以下代码实现它:
void create_shared_spsc_queue(size_t sz)
{
using char_alloc = boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager>;
using shared_string = boost::interprocess::basic_string<char, std::char_traits<char>, char_alloc>;
using string_alloc = boost::interprocess::allocator<shared_string, boost::interprocess::managed_shared_memory::segment_manager>;
using ring_buffer_dynamic = boost::lockfree::spsc_queue<shared_string, boost::lockfree::allocator<string_alloc> >;
ring_buffer_dynamic *queue_dynamic_;
boost::interprocess::managed_shared_memory segment_(boost::interprocess::open_only, "MySharedMemroy");
string_alloc string_alloc_(segment_.get_segment_manager());
queue_dynamic_ = segment_.construct<ring_buffer_dynamic>(rbuff_name)(string_alloc_, sz);
}
But this throws compilation error:但这会引发编译错误:
/usr/include/boost/interprocess/detail/named_proxy.hpp:85:7: error: no matching function for call to ‘boost::lockfree::spsc_queue<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char,
boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >,
boost::lockfree::allocator<boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long int, long unsigned int, 0>, 0>,
boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char,
boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long int, long unsigned int, 0>, 0>, boost::interprocess::iset_index> > > >::spsc_queue(boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char,
boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >&, int)’
85 | { ::new((void*)mem, boost_container_new_t())T(boost::forward<Args>(get<IdxPack>(args_))...); }
I can understand its related to issue with allocators, but I can't seem to resolve it, with my limited understanding of allocators.我可以理解它与分配器问题有关,但由于我对分配器的了解有限,我似乎无法解决它。 How can I implement this?我该如何实施?
For posterity: I figured, I was calling the ctor of spsc_queue in wrong order of arguments.为了后代:我想,我以 arguments 的错误顺序调用 spsc_queue 的 ctor。 The following works:以下作品:
queue_dynamic_ = segment_.construct<ring_buffer_dynamic>(rbuff_name)(sz, string_alloc_);
Source: https://www.boost.org/doc/libs/1_80_0/doc/html/boost/lockfree/spsc_queue.html资料来源: https://www.boost.org/doc/libs/1_80_0/doc/html/boost/lockfree/spsc_queue.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.