如题,我试了一下boost::interprocess里vector的模板放boost::interprocess::string失败了,想知道为什么눈_눈
普遍的做法都要重新定义string并指定分配器
1.
typedef boost::interprocess::allocator<std::string, boost::interprocess::managed_shared_memory::segment_manager> shmem_allocator;
typedef boost::interprocess::vector<std::string, shmem_allocator> shmem_vector;
2.
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 shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory shm(create_only, "MySharedMemory", 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);
3.
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 shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory shm(create_only, "MySharedMemory", 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);
希望有帮助
https://www.codenong.com/12980716/
https://www.dandelioncloud.cn/article/details/1533326532022136834
http://www.predream.org/index.php/show-335-1450-1.html