您可以使用以下方法
查看此演示
Live On Coliru
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/container/scoped_allocator.hpp>
#include <iostream>
#include <vector>
namespace Shared {
namespace bip = boost::interprocess;
namespace bc = boost::container;
using Segment = bip::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = bc::scoped_allocator_adaptor<bip::allocator<T, Manager> >;
template <typename T> using Vector= bip::vector<T, Alloc<T> >;
Segment open() { return { bip::open_or_create, "MySharedMemory", 10ul<<20 }; }
template <typename Alloc = Alloc<void> >
struct Foo {
using allocator_type = typename Alloc::template rebind<Foo>::other;
using Ints = bip::vector<int, typename Alloc::template rebind<int>::other>;
Foo(int id, float time, bool isFoo = false, std::initializer_list<float> floats = {}, Ints data = {})
: id(id), time(time), isFoo(isFoo), vectorData(std::move(data))
{
std::copy_n(floats.begin(), std::min(floats.size(), 7ul), myArray);
}
template <typename OA, typename A>
Foo(Foo<OA> const& other, A alloc = {})
: id(other.id), time(other.time), isFoo(other.isFoo),
vectorData(other.vectorData.begin(), other.vectorData.end(), alloc)
{
std::copy(std::begin(other.myArray), std::end(other.myArray), myArray);
}
int id;
float time;
bool isFoo;
float myArray[7] = {};
Ints vectorData;
};
template <typename A>
std::ostream& operator<<(std::ostream& os, Foo<A> const& f) {
os << "{"
<< f.id << ", "
<< std::fixed << f.time << ", "
<< std::boolalpha << f.isFoo << ", "
<< "[";
std::copy(std::begin(f.myArray), std::end(f.myArray), std::ostream_iterator<float>(std::cout, ";"));
os << "], [";
std::copy(std::begin(f.vectorData), std::end(f.vectorData), std::ostream_iterator<int>(std::cout, ";"));
return os << "] }";
}
}
using Foo = Shared::Foo<std::allocator<void> >;
using InterFoo = Shared::Foo<>;
using InterFoos = Shared::Vector<InterFoo>;
using Ints = Shared::Vector<int>;
int main() {
auto segment = Shared::open();
auto& inter_foos = *segment.find_or_construct<InterFoos>("InterFoos")(segment.get_segment_manager());
// you can directly append to the shared memory vector
int nextid = inter_foos.size();
inter_foos.push_back({++nextid, 0, true, {.1,.2,.3,.4,.5,.6,.7}, Ints ({10,20,30}, segment.get_segment_manager()) });
// or copy from a non-shared vector:
std::vector<Foo> const local {
{++nextid, 0, true, {.1,.2,.3,.4,.5,.6,.7}, {10,20,30} },
{++nextid, 1, true, {.2,.3,.4,.5,.6,.7,.8}, {20,30,40} },
{++nextid, 2, true, {.3,.4,.5,.6,.7,.8,.9}, {30,40,50} },
};
for (auto& local_foo : local)
inter_foos.emplace_back(local_foo);
// print the current contents
for (auto& foo : inter_foos)
std::cout << foo << "\n";
}
打印:
{1, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
{2, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
{3, 1.000000, true, [0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;], [20;30;40;] }
{4, 2.000000, true, [0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;0.900000;], [30;40;50;] }
第二次运行:
{1, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
{2, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
{3, 1.000000, true, [0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;], [20;30;40;] }
{4, 2.000000, true, [0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;0.900000;], [30;40;50;] }
{5, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
{6, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
{7, 1.000000, true, [0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;], [20;30;40;] }
{8, 2.000000, true, [0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;0.900000;], [30;40;50;] }
作用域分配器
请注意:
for (auto& local_foo : local)
inter_foos.emplace_back(local_foo);
之所以有效,是因为Boost的容器实现支持并传递作用域分配器。如果你没有使用它,事情会是这样的:
Coliru现场直播
template <typename T> using Alloc = bip::allocator<T, Manager>;
// ...
for (auto& local_foo : local)
inter_foos.emplace_back(local_foo, segment.get_segment_manager());
使用映射文件,因为COLIRU上不支持共享内存