【发布时间】:2015-11-17 20:55:30
【问题描述】:
在我的程序中,我使用 fork 创建了一些子进程,我想在它们之间共享 std::vector。 我知道 mmap,我尝试使用自己的分配器,它使用 mmap。
我的分配器类在这里: https://gist.github.com/ADKosm/798bf4caebbc5e087d86
如果我用这个分配器创建vector<int>:std::vector<int, mmap_allocator<int> > - 所有进程都可以操作这个向量的单一副本。
但如果我尝试使用std::vector<std::string, mmap_allocator<std::string> > - 每个进程只能访问它的容器副本。
我也尝试使用
#define shared_string std::basic_string<char, std::char_traits<char>, mmap_allocator<char> >
std::vector<shared_string, mmap_allocator<shared_string> >
但它也不起作用。
如何在分叉的进程之间共享这个容器?
PS。抱歉,我只是在学习英语
【问题讨论】:
-
std::string还分配不使用 mmap_allocator 的内部缓冲区。所以你可能需要用你的分配器类型定义string。但我不确定std::string是否支持它。我从来没有尝试过。您需要阅读std::string构造函数文档。
标签: c++ string vector shared-memory mmap