Search code examples
c++winapistlboost-interprocess

STL containers in shared memory (Windows)


I'm working on an application that is composed of 2 processes that have to share some data structures. These classes are organized in different libraries and the libraries are used in different applications.

  • My first attempt was to use Named Shared Memory in Win32 but the problem here is that I can't use STL containers.
  • Looking for a solution, I've found Boost Interprocess and, if I understand, I have to change all the STL containers, in my classes, to "STL" Boost Interprocess containers.

So, is there any other method to share classes or structures using STL (in Windows)? The goal is to not create too much dependence from Boost in our libraries.

Thanks


Solution

  • The reason that Boost.Interprocess has its own container classes is that most current implementations do not fully support the standard in regard of allocators.

    The Boost.Interprocess container classes are fully-compliant implementations of the standard containers, so you can use them as drop-in replacements for the standard containers and switch to your vendor-supplied containers when they are fixed to support the standard allocator protocol.

    I'm not sure whether I can explain it any better than the documentation, but the biggest problem is that shared memory is mapped into different processes at different virtual addresses, so any pointers used within the container must be a relative pointer (e.g. from the beginning of the segment). The standard allocator model supports this, but the container must also support this by using Allocator::pointer instead of T *. When all implementations are fixed to support this, the Boost.Interprocess containers will no longer be necessary.

    Alternatively, you may be able to map the memory at the same virtual address in each process; if you do this then you can use your existing implementation's containers, since raw pointers will work correctly.