[英]How get list of named objects in boost interprocess shared memory
According to guide on Boost docs 根据Boost文档指南
It is possible to create named vectors of some type (for instance double) 可以创建某种类型的命名向量(例如double)
using namespace boost::interprocess;
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
//Initialize shared memory STL-compatible allocator
const ShmemAllocator alloc_inst (segment.get_segment_manager());
//Construct a vector named "values_A" in shared memory with argument alloc_inst
MyVector *vA= segment.construct<MyVector>("values_A")(alloc_inst);
MyVector *vB= segment.construct<MyVector>("values_B")(alloc_inst);
for(int i = 0; i < 100; ++i) //Insert data in the vector
vA->push_back(i);
And then if client process knows the names of shared objects (" values_A " and " values_B ") it is easy to access them. 然后,如果客户端进程知道共享对象的名称(“ values_A ”和“ values_B ”),则很容易访问它们。
managed_shared_memory segment(open_only, "MySharedMemory");
MyVector *vA_client = segment.find<MyVector>("values_A").first;
MyVector *vB_client = segment.find<MyVector>("values_B").first;
//Use vector in reverse order
std::sort(vA_client->rbegin(), vA_client->rend());
But if client doesn't know the names of those objects? 但如果客户端不知道这些对象的名称?
How to get list of those objects? 如何获取这些对象的列表? {" values_A " , " values_B "}. {“ values_A ”,“ values_B ”}。
If there was some objects of other type (" MyVector2 ") registered (named " intA ", " intB ") - How would you filter only those, whose type is MyVector ? 如果有一些其他类型的对象(“ MyVector2 ”)已注册(名为“ intA ”,“ intB ”) - 您将如何仅过滤那些类型为MyVector的对象 ?
I have a suspicion that it has to do with method named_begin and named_end but I don't know how to use it. 我怀疑它与方法named_begin和named_end有关,但我不知道如何使用它。
Thank You for your help :) 谢谢您的帮助 :)
These methods are part of the segment manager interface. 这些方法是段管理器接口的一部分。
Beware of race conditions when using these directly. 直接使用时请注意竞争条件。
find_or_construct
does something else thanfind
andconstruct
sequentially in a multi-process situation.find_or_construct
除了在多进程情况下按顺序find
和construct
之外还执行其他操作。
Example: 例:
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <iostream>
namespace bip = boost::interprocess;
using Shared = bip::managed_mapped_file;
using Manager = Shared::segment_manager;
template <typename T> using Alloc = bip::allocator<T, Manager>;
template <typename T> using V = boost::container::vector<T, Alloc<T> >;
int main() {
bip::managed_mapped_file mmf(bip::open_or_create, "test.bin", 10ull<<10);
auto* segment = mmf.get_segment_manager();
for (auto it = segment->named_begin(); it != segment->named_end(); ++it) {
std::cout << "before: " << std::string(it->name(), it->name_length()) << "\n";
}
for (auto next : { "foo", "bar", "qux" }) {
if (!mmf.find<V<int> >(next).first)
{
mmf.find_or_construct<V<int> >(next)(segment);
break;
}
}
for (auto it = segment->named_begin(); it != segment->named_end(); ++it) {
std::cout << "after: " << std::string(it->name(), it->name_length()) << "\n";
}
}
Prints (first run): 打印(首次运行):
after: foo
Second run: 第二轮:
before: foo
after: bar
after: foo
Third run: 第三轮:
before: bar
before: foo
after: bar
after: foo
after: qux
Fourth (and later) run: 第四次(及以后)运行:
before: bar
before: foo
before: qux
after: bar
after: foo
after: qux
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.