Boost.Interprocess 内存位置
Boost.Interprocess memory location
在 Boost.Interprocess 文档 Where is this being allocated? 中指出 Boost.Interprocess 容器同时使用两种机制放置在共享内存中:
- Boost.Interprocess construct<>, find_or_construct<>... functions. These functions place a C++ object in the shared
memory. But this places only the object, not the memory that this
object may allocate dynamically.
- Shared memory allocators. These allow allocating shared memory portions so that containers can allocate dynamically fragments
of memory to store newly inserted elements.
有什么用例 boost.vector 内部内存位于当前进程中,但使用共享内存分配器以便将元素放置在共享内存中?
如果我想将此结构共享给另一个进程:
struct Shared
{
vector<string> m_names;
vector<char> m_data;
};
我想我希望其他进程可以访问这些向量,以便它可以对其进行迭代,对吗?
好吧,您无法从其他进程访问向量本身,但您可以访问元素(因此在您的示例中是字符串),例如通过指针
find_or_construct
和朋友都是为自己直接配的。
分配器将被传递给库类型,以类似的方式进行内部分配。否则,共享内存中只有 "control structure"(例如,典型的 std::string
为 16 个字节),而不是标准库容器内部分配的所有相关数据。
在 Boost.Interprocess 文档 Where is this being allocated? 中指出 Boost.Interprocess 容器同时使用两种机制放置在共享内存中:
- Boost.Interprocess construct<>, find_or_construct<>... functions. These functions place a C++ object in the shared memory. But this places only the object, not the memory that this object may allocate dynamically.
- Shared memory allocators. These allow allocating shared memory portions so that containers can allocate dynamically fragments of memory to store newly inserted elements.
有什么用例 boost.vector 内部内存位于当前进程中,但使用共享内存分配器以便将元素放置在共享内存中?
如果我想将此结构共享给另一个进程:
struct Shared
{
vector<string> m_names;
vector<char> m_data;
};
我想我希望其他进程可以访问这些向量,以便它可以对其进行迭代,对吗?
好吧,您无法从其他进程访问向量本身,但您可以访问元素(因此在您的示例中是字符串),例如通过指针
find_or_construct
和朋友都是为自己直接配的。
分配器将被传递给库类型,以类似的方式进行内部分配。否则,共享内存中只有 "control structure"(例如,典型的 std::string
为 16 个字节),而不是标准库容器内部分配的所有相关数据。