【发布时间】:2015-05-28 11:51:12
【问题描述】:
我正在尝试使用 boost::interprocess 在进程之间共享数据并利用 shared_ptr 进行生命周期管理。我有一个驻留在共享内存中的地图,两个进程应该访问它。
boost::shared_ptr<boost::interprocess::managed_mapped_file> segment =
boost::make_shared<boost::interprocess::managed_mapped_file>
(boost::interprocess::open_or_create,
"./some-mmap.txt", //file name
65536); //segment size in bytes
pair_allocator_type alloc_inst(segment->get_segment_manager());
elements = boost::interprocess::make_managed_shared_ptr(
segment->find_or_construct<map_type>("elements")
(std::less<IdType>(), alloc_inst),
*segment
);
在一个测试程序中,我有一个父进程和一个子进程,它们基本上都使用上面的代码。因此,它们使用相同的底层文件、相同的共享对象名称(“元素”)、相同的类型等。
但是,我注意到每当子进程死亡时,集合的大小就会下降到 0。奇怪。我进行了调查,似乎它与elements 的破坏有关(当此共享指针超出范围时)。每当elements 超出范围时,底层集合的大小就会变为0。
我还看到elements 在父进程和子进程中都有use_count 正好1。对于父母来说这是有道理的,但我不明白为什么孩子会这样。我的假设是当 Child 进程死亡时,use_count 会降为 0,然后集合被清除。
我想要的是当子进程死亡时,指向的对象(地图)不会被破坏。我不应该假设哪些进程是活跃的,哪些不是。
- 我是否以错误的方式初始化
boost::interprocess::shared_ptr? - 我是否完全错过了此指针的语义 --> 它是否仅用于管理共享内存对象的生命周期仅在一个进程内而不是跨进程?
- 如何拥有一个shared_ptr,它的use_count 是跨进程共享的?
编辑 - 收集说明
elements 是一个boost::interprocess::map,它将某个IdType 映射到指向ShmemType 的共享内存共享指针。当 Child 进程死亡时,elements 的大小会降为 0。
typedef boost::interprocess::managed_mapped_file::segment_manager segment_manager_type;
typedef std::pair<const IdType, ShmemType::pointer_type> pair_type;
typedef boost::interprocess::allocator<pair_type, segment_manager_type> pair_allocator_type;
typedef boost::interprocess::map<IdType, ShmemType::pointer_type, std::less<IdType>, pair_allocator_type> map_type;
编辑 - 来自 boost 文档的示例
我从 boost 文档中获取了示例并对其进行了扩展,以追踪我最初问题的根本原因。
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/smart_ptr/shared_ptr.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
#include <cstdlib> //std::system
using namespace boost::interprocess;
typedef allocator<int, managed_mapped_file::segment_manager> ShmemAllocator;
typedef vector<int, ShmemAllocator> MyVector;
#include <iostream>
//Main function. For parent process argc == 1, for child process argc == 2
int main(int argc, char *argv[])
{
if(argc == 1){ //Parent process
//Create a new segment with given name and size
managed_mapped_file segment(open_or_create, "./a_MySharedMemory.txt", 65536);
//Initialize shared memory STL-compatible allocator
const ShmemAllocator alloc_inst (segment.get_segment_manager());
// MyVector* elements = segment.find_or_construct<MyVector>("some-vector") //object name
// (alloc_inst);
typedef boost::interprocess::managed_shared_ptr<MyVector, boost::interprocess::managed_mapped_file>::type map_pointer_type;
map_pointer_type elements = boost::interprocess::make_managed_shared_ptr(
segment.find_or_construct<MyVector>("some-vector") //object name
(alloc_inst),
segment
);
for(int i = 0; i < 100; ++i) //Insert data in the vector
elements->push_back(i);
std::cout << elements->size() << std::endl;
std::cout << elements->at(0) << std::endl;
std::cout << elements->at(30) << std::endl;
//Launch child process
std::string s(argv[0]); s += " child ";
if(0 != std::system(s.c_str()))
return 1;
std::cout << elements->size() << std::endl;
std::cout << elements->at(0) << std::endl;
std::cout << elements->at(30) << std::endl;
}
else{ //Child process
//Open the managed segment
managed_mapped_file segment(open_only, "./a_MySharedMemory.txt");
const ShmemAllocator alloc_inst (segment.get_segment_manager());
typedef boost::interprocess::managed_shared_ptr<MyVector, boost::interprocess::managed_mapped_file>::type map_pointer_type;
map_pointer_type elements = boost::interprocess::make_managed_shared_ptr(
segment.find_or_construct<MyVector>("some-vector") //object name
(alloc_inst),
segment
);
// MyVector* elements = segment.find_or_construct<MyVector>("some-vector") //object name
// (alloc_inst);
//Use vector in reverse order
std::sort(elements->rbegin(), elements->rend());
}
return 0;
}
在这种情况下,在子进程死亡后,向量在父进程中的大小 == 0。如果我使用原始指针 (MyVector* elements = segment.find_or_construct...),那么集合可以在父进程中按预期使用。
所以我仍然对共享指针的行为有疑问
【问题讨论】:
-
"集合的大小降至 0" - 哪个集合?
-
我在文中澄清了。
elements->size()在子进程死亡后在父进程中给出 0,即使在子进程中没有修改elements映射。 -
在子进程死亡之前?
-
是一个。我所做的测试是父添加一个元素,生成一个子进程,子进程读取这个元素,子进程死亡,父进程读取那个元素。最后一次读取应该成功,但它失败了(
elements映射中没有任何内容)。 -
大小下降到 0 没有意义。这不是智能指针所做的,无论如何都不是没有自定义删除器。您应该检查地图实际上是同一个对象(并且它仍然存在!)。您是否使该细分市场保持足够长的时间? (例如,将分段设为全局以进行测试)
标签: c++ boost shared-ptr boost-interprocess