From: Jonathan Turkanis (technews_at_[hidden])
Date: 2005-08-02 16:58:51


Jason Hise wrote:
> Jonathan Turkanis wrote:
>
>> Jason Hise wrote:
>>
>>
>>> So to avoid the whole mess regarding the DCLP, the best approach
>>> would probably be eager initialization, before the program enters
>>> a multi-threaded context. Is initializing from the constructor of
>>> a namespace-scope global enough to ensure this?
>>>
>>>
>> I don't think so, since you might want to access singletons from
>> static initializers in other translation units.
>>
>> Have you considered using call_once:
>> http://www.boost.org/doc/html/call_once.html?
>>
> Why does it require that the function which is called once not be
> allowed to throw? I am not sure that I can meet this requirement, as
> I
> may need to dynamically allocate memory.

Whoops! I forgot about that. In the example I gave, you can use boost::optional.

If the construction of your object can throw, I think you can use the above
method to construct a mutex (in a buffer using placement new) and use the mutex
to synchronize construction of the object.

Jonathan