Boost logo

Boost :

From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2006-10-08 05:39:55


Anthony Williams wrote:
> Here's a win32-only static once_init implementation. This could be used as a
> general purpose static-init template, or as a basis for making boost::mutex
> work both as a static or non-static object, with a default constructor.
>
> Comments please,

Anthony, I did not yet have had the time to walk through all details
of your proposal, but the main problem why this isn't thread safe is:

void f()
{
     static once_init<A> a;
     a->f();

}

class once_init does have a user declared constructor and a destructor!
And this definitely is _not_thread safe.

I am even unsure if

void foo()
{
     static int n = 42;
     m = n;
}

is thread safe, since:

6.7/4:
" ... A local object of POD type with static storage duration
initialized with constant-expressions is initialized before its
block is first entered. ..."

To me this reads, as if the compiler would be allowed to put
the initialization into the function entry code.

This would rule out local static objects completely for
MT safe programs.

Perhaps we should try to discuss this at comp.lang.c++.pthreads ?

Roland


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