c++boostshared-memoryboost-interprocess

Is boost::managed_shared_memory using a file on my hard drive?


When I create a new boost::interprocess::managed_shared_memory, I can see a file appear in C:\ProgramData\boost_interprocess\1571641094\NAME_OF_SHARED_MEMORY which size seems to match that of the shared memory created.

My understanding from the documention is that there are two widely used objects usable to share memory between processes (within managed_shared_memory's scope):

The managed_shared_memory uses the basic_managed_shared_memory implementation. I assumed this implementation is proper shared memory and not a memory mapped file.

Seeing that it uses a file puzzles me. Are both of these managed shared memory based on a memory mapped file implementation ?

Is the only boost shared memory solution on Windows that avoids memory mapped files the windows_shared_memory ?

NB : I am on Windows 10, using VC++ on VS2013.

Code sample that can reproduce the behaviour of creating a file in ProgramData when using a managed_shared_memory :

#include <boost/interprocess/managed_shared_memory.hpp>

int main(int argc, char *argv[])
{
  boost::interprocess::permissions permissions;
  permissions.set_unrestricted();

  boost::interprocess::managed_shared_memory* sharedMemory;
  sharedMemory = new boost::interprocess::managed_shared_memory(
  {
    boost::interprocess::open_or_create,
    "NAME_OF_SHARED_MEMORY",
    400000,
    0,
    permissions
  }
  );

  return 0;
}

Solution

  • Your assumptions are correct. The 'Emulation for systems without shared memory objects' section from the Boost documentation explains what is going on:

    Boost.Interprocess provides portable shared memory in terms of POSIX semantics. Some operating systems don't support shared memory as defined by POSIX:

    • Windows operating systems provide shared memory using memory backed by the paging file but the lifetime semantics are different from the ones defined by POSIX (see Native windows shared memory section for more information).

    ...

    In those platforms, shared memory is emulated with mapped files created in a "boost_interprocess" folder created in a temporary files directory. In Windows platforms, if "Common AppData" key is present in the registry, "boost_interprocess" folder is created in that directory (in XP usually "C:\Documents and Settings\All Users\Application Data" and in Vista "C:\ProgramData"). For Windows platforms without that registry key and Unix systems, shared memory is created in the system temporary files directory ("/tmp" or similar).

    Because of this emulation, shared memory has filesystem lifetime in some of those systems.

    As you already pointed out, as an alternative, you can use native windows shared memory objects (using windows_shared_memory). It will use a shared memory object backed by the pagefile instead of a file being created in C:\ProgramData. In general, this will not use the filesystem (see: https://stackoverflow.com/a/6215317/79111). In case portability is not a concern, it is probably a better approach since it allows interoperability with other applications that use shared memory but do not rely on Boost.Interprocess.