#include #include #include #include #include #include template struct segment_allocator: boost::interprocess::allocator { typedef boost::interprocess::allocator super; segment_allocator():super(SegmentManager::get()){} template segment_allocator(const segment_allocator& x):super(x){} super& base(){return *this;} template struct rebind{ typedef segment_allocator other; }; friend void swap(segment_allocator& x,segment_allocator& y) { swap(x.base(),y.base()); } }; template struct segment_holder_class:boost::flyweights::holder_marker { static C& get() { static C* pc=SegmentManager::get()-> find_or_construct(typeid(segment_holder_class).name())(); return *pc; } }; template struct segment_holder:boost::flyweights::holder_marker { template struct apply { typedef segment_holder_class type; }; }; template struct labeled_recursive_mutex { typedef boost::interprocess::named_recursive_mutex mutex_type; operator mutex_type&() { static mutex_type mutex( boost::interprocess::open_or_create,Name::get()); return mutex; } }; template struct labeled_locking:boost::flyweights::locking_marker { typedef labeled_recursive_mutex mutex_type; typedef boost::interprocess::scoped_lock< typename labeled_recursive_mutex::mutex_type> lock_type; }; #include #include #include #include #include using namespace boost::flyweights; namespace ipc=boost::interprocess; struct memory_dat_segment_manager { typedef ipc::managed_mapped_file::segment_manager type; static type* get() { static ipc::managed_mapped_file* segment= new ipc::managed_mapped_file(ipc::open_or_create,"memory.dat",409600); return segment->get_segment_manager(); } }; struct mutex_name { static const char* get(){return "7AD6D8E8-320B-12DC-82CF-F0B655D995AF";} }; typedef ipc::basic_string< char,std::char_traits, segment_allocator > shm_string; typedef flyweight< shm_string, hashed_factory< boost::hash, std::equal_to, segment_allocator >, labeled_locking, segment_holder > shm_string_flyweight; int main() { shm_string_flyweight x("hello"),y("hello"),z("bye"); assert(x==y); assert(x!=z); }