mirror of
https://github.com/boostorg/interprocess.git
synced 2026-01-22 05:12:44 +00:00
Source breaking: A shared memory object is now used including shared_memory_object.hpp header instead of shared memory.hpp. ABI breaking: Changed global mutex when initializing managed shared memory and memory mapped files. This change tries to minimize deadlocks. Source breaking: Changed shared memory, memory mapped files and mapped region's open mode to a single boost::interprocess::mode_t type. Added extra WIN32_LEAN_AND_MEAN before including DateTime headers to avoid socket redefinition errors when using Interprocess and Asio in windows. ABI breaking: mapped_region constructor no longer requires classes derived from memory_mappable, but classes the fulfill the MemoryMappable concept. Added in-place reallocation capabilities to basic_string. ABI breaking: Reimplemented and optimized small string optimization. The narrow string class has zero byte overhead with an internal 11 byte buffer in 32 systems! Added move semantics to containers. Experimental and not documented yet. Improves performance when using containers of containers. ABI breaking: End nodes of node containers (list, slist, map/set) are now embedded in the containers instead of allocated using the allocator. This allows no-throw move-constructors and improves performance. ABI breaking: slist and list containers now have constant-time size() function. The size of the container is added as a member. [SVN r35618]
70 lines
2.8 KiB
C++
70 lines
2.8 KiB
C++
#include <boost/interprocess/detail/config_begin.hpp>
|
|
#include <boost/interprocess/detail/workaround.hpp>
|
|
|
|
#include <boost/interprocess/managed_shared_memory.hpp>
|
|
#include <boost/interprocess/containers/vector.hpp>
|
|
#include <boost/interprocess/containers/string.hpp>
|
|
#include <boost/interprocess/allocators/allocator.hpp>
|
|
#include <cassert>
|
|
|
|
int main ()
|
|
{
|
|
using namespace boost::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 old shared memory and create new one
|
|
shared_memory_object::remove("myshm");
|
|
managed_shared_memory shm(create_only, "myshm", 10000);
|
|
|
|
//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(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(), move(string_to_compare));
|
|
|
|
//Destroy vector. This will free all strings that the vector contains
|
|
shm.destroy_ptr(myshmvector);
|
|
return 0;
|
|
}
|
|
|
|
#include <boost/interprocess/detail/config_end.hpp>
|