int main () { using namespace hexerboost::interprocess; //Typedefs typedef managed_shared_memory::segment_manager SegmentManager; typedef allocator<char, SegmentManager> CharAllocator; typedef basic_string<char, std::char_traits<char> ,CharAllocator> MyShmString; typedef allocator<MyShmString, SegmentManager> StringAllocator; typedef vector<MyShmString, StringAllocator> MyShmStringVector; //Remove shared memory on construction and destruction struct shm_remove { //<- #if 1 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); } ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); } #else //-> shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } //<- #endif //-> } remover; //<- (void)remover; //-> //<- #if 1 managed_shared_memory shm(create_only, test::get_process_id_name(), 65536); #else //-> managed_shared_memory shm(create_only, "MySharedMemory", 10000); //<- #endif //-> //Create allocators CharAllocator charallocator (shm.get_segment_manager()); StringAllocator stringallocator(shm.get_segment_manager()); //Create a vector of strings in shared memory. MyShmStringVector *myshmvector = shm.construct<MyShmStringVector>("myshmvector")(stringallocator); //Insert 50 strings in shared memory. The strings will be allocated //only once and no string copy-constructor will be called when inserting //strings, leading to a great performance. MyShmString string_to_compare(charallocator); string_to_compare = "this is a long, long, long, long, long, long, string..."; myshmvector->reserve(50); for(int i = 0; i < 50; ++i){ MyShmString move_me(string_to_compare); //In the following line, no string copy-constructor will be called. //"move_me"'s contents will be transferred to the string created in //the vector myshmvector->push_back(hexerboost::move(move_me)); //The source string is in default constructed state assert(move_me.empty()); //The newly created string will be equal to the "move_me"'s old contents assert(myshmvector->back() == string_to_compare); } //Now erase a string... myshmvector->pop_back(); //...And insert one in the first position. //No string copy-constructor or assignments will be called, but //move constructors and move-assignments. No memory allocation //function will be called in this operations!! myshmvector->insert(myshmvector->begin(), hexerboost::move(string_to_compare)); //Destroy vector. This will free all strings that the vector contains shm.destroy_ptr(myshmvector); return 0; }
int main () { using namespace boost::interprocess; //Typedefs typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator; typedef basic_string<char, std::char_traits<char>, CharAllocator> MyShmString; typedef allocator<MyShmString, managed_shared_memory::segment_manager> StringAllocator; typedef vector<MyShmString, StringAllocator> MyShmStringVector; //Open shared memory //Remove shared memory on construction and destruction struct shm_remove { //<- #if 1 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); } ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); } #else //-> shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } //<- #endif //-> } remover; //<- (void)remover; //-> //<- #if 1 managed_shared_memory shm(create_only, test::get_process_id_name(), 10000); #else //-> managed_shared_memory shm(create_only, "MySharedMemory", 10000); //<- #endif //-> //Create allocators CharAllocator charallocator (shm.get_segment_manager()); StringAllocator stringallocator(shm.get_segment_manager()); //This string is in only in this process (the pointer pointing to the //buffer that will hold the text is not in shared memory). //But the buffer that will hold "this is my text" is allocated from //shared memory MyShmString mystring(charallocator); mystring = "this is my text"; //This vector is only in this process (the pointer pointing to the //buffer that will hold the MyShmString-s is not in shared memory). //But the buffer that will hold 10 MyShmString-s is allocated from //shared memory using StringAllocator. Since strings use a shared //memory allocator (CharAllocator) the 10 buffers that hold //"this is my text" text are also in shared memory. MyShmStringVector myvector(stringallocator); myvector.insert(myvector.begin(), 10, mystring); //This vector is fully constructed in shared memory. All pointers //buffers are constructed in the same shared memory segment //This vector can be safely accessed from other processes. MyShmStringVector *myshmvector = shm.construct<MyShmStringVector>("myshmvector")(stringallocator); myshmvector->insert(myshmvector->begin(), 10, mystring); //Destroy vector. This will free all strings that the vector contains shm.destroy_ptr(myshmvector); return 0; }