Boost logo

Boost :

Subject: Re: [boost] [interprocess] default constructors
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2008-11-30 14:21:05


Ion Gaztañaga wrote:

> Sorry, I don't understand very well this point. Shared memory is never
> destroyed until explicitly removed, but it must be reopened if the
> shared_memory_object object is destroyed:
>
> //Imagine a default constructor is available:
> managed_shared_memory global_shm;
>
>
> void func(bool false_is_open_true_is_create)
> {
> if(false_is_open_true_is_create){
> managed_shared_memory shm(create_only, "name", MemSize);
> global_shm.swap(shm);
> }
> else{
> managed_shared_memory shm(open_only, "name", MemSize);
> global_shm.swap(shm);
> }
> //global_shm is usable here because it has been swapped
> //with a correctly opened instance
> global_shm.create<>(...);
> }

Why not simply delay the construction of the managed shared memory
segment until you have the required arguments to initialize it?

The original poster said he could have used pointers and dynamic memory
allocation to achieve that, but that's a fairly inefficient way of doing it.
It is perfectly possible to simply use a POD with compatible size and
alignment as a global variable, then construct it as the desired object
once possible.
This is basically what boost::optional does, except it also uses a
boolean to know whether destruction must be performed or not.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk