I am using a shared vector to share objects across memory:
using ShmemAllocator = bip::allocator<T, bip::managed_shared_memory::segment_manager>;
using MyVector = bip::vector<T, ShmemAllocator>;
bip::permissions perm;
perm.set_unrestricted();
segment.reset(new bip::managed_shared_memory(bip::open_or_create, shared_memory_name, numBytes, 0, perm));
const ShmemAllocator alloc_inst(segment->get_segment_manager());
vec = segment->find_or_construct<MyVector>(shared_vector_name)(alloc_inst);
Note the vector is created within a managed_shared_memory
object and this is created by specifying a number of bytes, not number of vector elements.
I then write elements to the vector:
int write(const std::vector<T>& vec)
{
bip::scoped_lock<bip::named_mutex> lock(*sdc.mutex);
for(const auto& item : vec)
{
sdc.vec->push_back(item);
}
sdc.cond_empty->notify_all();
}
What is the safest way to check whether I have enough space to write all my elements, prior to writing? I would really like to avoid simply assigning a large number of bytes and hoping I never hit it!