سؤال

when i declare my queue as

typedef boost::lockfree::queue<MyMessage, boost::lockfree::fixed_sized<true>> MessageQueue

and i try to use the queue as a multiple-producer-single-consumer queue, i.e. post messages to it from multiple threads and poll the queue for messages from a single thread, the consumer thread gets locked. nothing moves forward.

but when i don't use the fixed size property, i.e. don't specify the template parameter fixed_sized<true> , the queue works fine. But in this case the queue actually does a memory allocation/deallocation which defeats the purpose of using a lockless queue.

So my question is "does boost::lockfree::queue becomes a single-producer-single-consumer queue if set with the property fixed_sized<true>?"

is there some different push/pop methods i need to use?

I want an MPMC queue which does not do memory allocation deallocation. if boost does not provide this, is there any other queue that i can use??

Thanks in advance,

هل كانت مفيدة؟

المحلول

So my question is does boost::lockfree::queue becomes a single-producer-single-consumer queue if set with the property fixed_size?

Answer: According to the docs, no. The member functions have different thread safety, but you can use the lockfree queue as a MRMW queue no matter if the policy is fixed_size<true> or fixed_size<false> (e.g. member functions push and pop are thread-safe).

See the comments to the OP for a discussion. Short version:

If I interpret the docs correctly, the fixed_size<true> policy implies the following changes:

  • If you call push and no further capacity is available, push fails and returns false.

  • The maximum capacity that can be set is limited typically to 216-2 elements.

  • As the capacity is not changed automatically, you'll have to manually set the capacity via the ctor queue(size_type) or the reserve member function. The default ctor will set the capacity to 0. (This implies that for the default capacity of 0, every push will fail.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top