2013-02-15 72 views
8

我在將boost::lockfree::queue<<T, fixed_sized<false>, ..> 置於共享內存中時出現問題。我需要它,因爲我必須要能插入超過65535個消息到隊列中,並fixed_sized隊列與65535
下面的代碼工作正常限制(但capacity<...>選項意味着fixed_sized<true>):共享內存中的boost :: lockfree :: queue遇到問題(boost 1.53,gcc 4.7.2/clang 3.0-6ubuntu3)

typedef boost::interprocess::allocator< 
    MessageT, 
    boost::interprocess::managed_shared_memory::segment_manager> 
     ShmemAllocator; 
typedef boost::lockfree::queue< 
    MessageT, 
    boost::lockfree::capacity<65535>, 
    boost::lockfree::allocator<ShmemAllocator> > 
     Queue; 
m_segment = new boost::interprocess::managed_shared_memory(
    boost::interprocess::create_only, segmentName, size); 
Queue* m_queue = m_segment->construct<Queue>(
    queueName)(
    m_segment->get_segment_manager()); 
... 
m_queue->bounded_push(message); 

的下面的代碼工作正常過(但它不使用共享內存):

boost::lockfree::queue<MessageT> q; 
.... 
q.bounded_push(message); 

但是當我嘗試把它結合起來:

typedef boost::interprocess::allocator< 
    MessageT, 
    boost::interprocess::managed_shared_memory::segment_manager> 
     ShmemAllocator; 
typedef boost::lockfree::queue< 
    MessageT, 
    boost::lockfree::allocator<ShmemAllocator> > 
     Queue; 
m_segment = new boost::interprocess::managed_shared_memory(
    boost::interprocess::create_only, segmentName, size); 
Queue* m_queue = m_segment->construct<Queue>(
    queueName)(
    m_segment->get_segment_manager()); 
... 
m_queue->bounded_push(message); 

失敗與以下日誌編譯:

In file included from src/model/Queue.h:16: 

In file included from /home/uppi/lib/include/boost/lockfree/queue.hpp:24: 

/home/uppi/lib/include/boost/lockfree/detail/freelist.hpp:171:28: error: no viable conversion from 'pointer' (aka 'offset_ptr<boost::lockfree::queue<PacketMessage, 
     boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, 
     boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::node, long, unsigned long, 0UL>') to 
     'boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, 
     boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, 
     boost::parameter::void_>::node *' 
        return Alloc::allocate(1); 
          ~~~~~~~~~~~~~~~~~ 

/home/uppi/lib/include/boost/lockfree/detail/freelist.hpp:157:20: note: in instantiation of function template specialization 'boost::lockfree::detail::freelist_stack<boost::lockfree::queue<PacketMessage, 
     boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, 
     boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::node, 
     boost::interprocess::allocator<boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, 
     boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, 
     boost::parameter::void_>::node, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned 
     long, 0>, 0>, iset_index> > >::allocate_impl<true>' requested here 
      return allocate_impl<Bounded>(); 


/home/uppi/lib/include/boost/lockfree/detail/freelist.hpp:89:20: note: in instantiation of function template specialization 'boost::lockfree::detail::freelist_stack<boost::lockfree::queue<PacketMessage, 
     boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, 
     boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::node, 
     boost::interprocess::allocator<boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, 
     boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, 
     boost::parameter::void_>::node, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned 
     long, 0>, 0>, iset_index> > >::allocate<true, true>' requested here 
     T * node = allocate<ThreadSafe, Bounded>(); 


/home/uppi/lib/include/boost/lockfree/queue.hpp:281:34: note: in instantiation of function template specialization 'boost::lockfree::detail::freelist_stack<boost::lockfree::queue<PacketMessage, 
     boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, 
     boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::node, 
     boost::interprocess::allocator<boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, 
     boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, 
     boost::parameter::void_>::node, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned 
     long, 0>, 0>, iset_index> > >::construct<true, true, PacketMessage, boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, 
     boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, 
     boost::parameter::void_, boost::parameter::void_>::node *>' requested here 
     node * n = pool.template construct<true, Bounded>(t, pool.null_handle()); 


/home/uppi/lib/include/boost/lockfree/queue.hpp:270:16: note: in instantiation of function template specialization 'boost::lockfree::queue<PacketMessage, 
     boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, 
     boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::do_push<true>' requested here 
     return do_push<true>(t); 


src/model/Queue.inl:4:18: note: in instantiation of member function 'boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, 
     boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, 
     boost::parameter::void_, boost::parameter::void_>::bounded_push' requested here 
     return m_queue->bounded_push(message);       

/home/uppi/lib/include/boost/interprocess/offset_ptr.hpp:450:4: note: candidate function 
    operator unspecified_bool_type() const 

請告訴我,我在共享內存中使用boost::lockfree::queueboost::lockfree::stack缺少

+0

您是否問過lockfree的作者Tim Blechmann呢?他通常很有幫助。 – eile 2013-02-15 19:56:59

+0

謝謝,我剛給他發了一封電子郵件 – uppi 2013-02-15 20:21:35

回答

14

限制爲65535元,出於兼容性考慮。如果您有單生產者,單消費者使用案例,則可能需要使用boost::lockfree::spsc_queue。但是這也不是動態大小的。

原因是限制是32位兼容性。對於64位平臺,人們可能能夠使boost.lockfree代碼使用32位而不是16位索引。但它需要一些不重要的更改才能正確實施。

+0

感謝您的解釋 – uppi 2013-02-19 20:18:33

+0

@timblechmann但是如果x86_64平臺已經有10年了,那爲什麼還沒有實現boost :: lockfree :: queue的64位實現? – Alex 2013-11-15 20:17:42

+0

@Alex只是讓你的手變髒,並提供一個補丁,它增加了一個策略來配置數據結構;) – timblechmann 2013-12-20 09:12:49