Question

I've discovered that passing a string pointer into a boost::lockfree::queue will cause a memory leak because the string pointer cannot be fully released.

Is the situation the same for a boost::lockfree::stack?

The requirement for a boost::lockfree::stack is:

  • T must have a copy constructor

If a regular string pointer cannot be used, is there any other way a string can be put into a boost::lockfree::stack?

Regular string

When I try this

boost::lockfree::stack<string> my_stack(128);

I get these errors

 BOOST_STATIC_ASSERT(boost::has_trivial_assign<T>::value);
 BOOST_STATIC_ASSERT(boost::has_trivial_destructor<T>::value);

which seems strange to me, probably because of my inexperience, because those are actually the requirements for boost::lockfree::queue which now strangely has no documentation

  • T must have a copy constructor
  • T must have a trivial assignment operator
  • T must have a trivial destructor
Was it helpful?

Solution

  1. The missing documentation is a Doxygen bug, the documentation page gets the name from a MACRO :(, and it is here: http://www.boost.org/doc/libs/1_55_0/doc/html/boost/lockfree/BOOST_NO_CXX1_idp100289128.html

  2. Indeed the stack appears to have the same element type requirements as the queue, despite the fact that the documentation doesn't mention it. What's worse, it seems that boost::string_ref hasn't been kept a POD either.

So my suggestion would be to store pointers into a "pool" of immutable string objects and just free the pool at the time when the queue is freed. This way, you just have to manage the lifetime of the queue in such a way that the memory consumption doesn't grow out of control.

In some ways this resembles implementing a poor-man's GC, and yes, it will force regular delays in the throughput to re-initialize the queue; In fairness is common to the field of lock-less memory management, and as the Boost Lockfree documentation states, most "viable" memory reclamation schemes in existence are patented.

You might have a look at libcds: Lock-free Concurrent Datastructures in case you want to toy with some of these algorithms. I think the author of that library has been in active conversation with Boost developers about proposing libcds for Boost, and it likely did not take place because of the patent issues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top