Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.

Using smart pointers with Boost.Intrusive containers

Requirements for smart pointers compatible with Boost.Intrusive

Boost.Intrusive hooks can be configured to use other pointers than raw pointers. When a Boost.Intrusive hook is configured with a smart pointer as an argument, this pointer configuration is passed to the containers. For example, if the following hook is configured with a smart pointer (for example, an offset pointer from Boost.Interprocess):

#include <boost/intrusive/list.hpp>
#include <boost/interprocess/offset_ptr.hpp>

using namespace boost::intrusive;
namespace ip = boost::interprocess;

class shared_memory_data
   //Declare the hook with an offset_ptr from Boost.Interprocess
   //to make this class compatible with shared memory
   :  public list_base_hook< void_pointer< ip::offset_ptr<void> > >
   int data_id_;

   int get() const   {  return data_id_;  }
   void set(int id)  {  data_id_ = id;    }

Any intrusive list constructed using this hook will be ready for shared memory, because the intrusive list will also use offset pointers internally. For example, we can create an intrusive list in shared memory combining Boost.Interprocess and Boost.Intrusive:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

//Definition of the shared memory friendly intrusive list
typedef list<shared_memory_data> intrusive_list_t;

int main()
   //Now create an intrusive list in shared memory:
   //nodes and the container itself must be created in shared memory
   const int MaxElem    = 100;
   const int ShmSize    = 50000;
   const char *ShmName  = get_shared_memory_name();
      //Erase all old shared memory
      ip::managed_shared_memory shm(ip::create_only, ShmName, ShmSize);

      //Create all nodes in shared memory using a shared memory vector
      //See Boost.Interprocess documentation for more information on this
      typedef ip::allocator
         < shared_memory_data, ip::managed_shared_memory::segment_manager>
      typedef ip::vector<shared_memory_data, shm_allocator_t> shm_vector_t;
      shm_allocator_t shm_alloc(shm.get_segment_manager());
      shm_vector_t *pshm_vect =

      //Initialize all the nodes
      for(int i = 0; i < MaxElem; ++i)    (*pshm_vect)[i].set(i);

      //Now create the shared memory intrusive list
      intrusive_list_t *plist = shm.construct<intrusive_list_t>(ip::anonymous_instance)();

      //Insert objects stored in shared memory vector in the intrusive list
      plist->insert(plist->end(), pshm_vect->begin(), pshm_vect->end());

      //Check all the inserted nodes
      int checker = 0;
      for( intrusive_list_t::const_iterator it = plist->begin(), itend(plist->end())
         ; it != itend; ++it, ++checker){
         if(it->get() != checker)   return 1;

      //Now delete the list and after that, the nodes
   return 0;

Not every smart pointer is compatible with Boost.Intrusive:

  • It must be compatible with C++11 std::pointer_traits requirements. Boost.Intrusive uses its own pointer_traits class to implement those features in both C++11 and C++03 compilers.
  • It must have the same ownership semantics as a raw pointer. This means that resource management smart pointers (like boost::shared_ptr) can't be used.

The conversion from the smart pointer to a raw pointer will be implemented as a recursive call to operator->() until the function returns a raw pointer.