I'm right now getting into shared memory using boost::interprocess
.
I've defined a few std::unordered_map
and std::unordered_set
types like in the following manner:
#include <boost/interprocess/allocators/allocator.hpp>
#include <unordered_map> // NOT the boost implementation ...
...
namespace ipc = boost::interprocess;
/**
* allocator type needed to construct maps in shared memory
*/
typedef ipc::allocator<std::pair<const size_t, std::string>,
ipc::managed_shared_memory::segment_manager> OBJ_MAP_ALLOCATOR;
/**
* map type to construct maps in shared memory
*/
typedef std::unordered_map<size_t,
std::string,
std::hash<size_t>,
std::equal_to<size_t>,
OBJ_MAP_ALLOCATOR> OBJ_MAP_TYPE;
I initialised them like this:
ipc::managed_shared_memory segment;
// allocate segment etc ...
OBJ_MAP_ALLOCATOR alloc_inst(segment.get_segment_manager());
objMap = segment.find_or_construct<OBJ_MAP_TYPE> (ipc::unique_instance)(alloc_inst);
This seems to work fine, i haven't found any problems during compile- or runtime (working on macOS, Apple LLVM version 9.1.0 (clang-902.0.39.1)
, with C++14 standard).
In the Boost documentation, only the Boost containers, or the interprocess-specific implementations are mentioned. Unfortunately, they do not seem to contain the unordered versions.
So, i wonder if there's anything problematic about using the default STL containers with the Boost allocators ? Maybe on a different platform ?
Any hint appreciated !
Update:
I was wondering if it was working in a different environment, so i wrote a minimal example on Coliru (which surprisingly works with std::string
):
http://coliru.stacked-crooked.com/a/91d1a143778cf3e9
See Question&Answers more detail:os