|
Boost : |
From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2005-09-13 13:50:45
Jason Hise <chaos_at_[hidden]> writes:
> John Maddock wrote:
>
>>It's possible to do what you want with call_once, but it's one of those
>>things you might think is impossible until someone shows you how, and then
>>it's embarrassingly easy, see
>>http://lists.boost.org/Archives/boost/2005/07/90756.php for an explanation.
>>
> In that example, where is the once_flag located? I would think that a
> similar race condition problem would exist when constructing and/or
> initializing that once_flag with BOOST_ONCE_INIT.
static objects undergo two-phase init. Firstly, there is static
initialization, which is to all-zero, or to a constant initializer. This
happens at or before program startup, before any code is run. Then there is
dynamic initialization, which is where things like constructors are run, or
initialization to the result of a function call. Dynamic initialization is
where the race conditions can happen.
once_flag is carefully designed so BOOST_ONCE_INIT is suitable for static
initialization. Typically it initializes the once_flag to all-zeros.
It is possible to design a mutex this way. pthreads mutexes have a static
initializer, for example. boost::mutex is not currently designed this way.
Anthony
-- Anthony Williams Software Developer Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk