Boost logo

Boost :

From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2006-12-13 13:38:13


Sean Huang wrote:
> I posted this about a year ago but got no definite answer from Robert.
> http://thread.gmane.org/gmane.comp.lib.boost.devel/132838/focus=132838
> I vaguely remember that there were similar discussions about this issue a
> few month back but do not know if it had reached any conclusion. Now my
> programs started crashing because of this problem and I am wondering if
> there is a good solution. Is making the function scope static objects static
> member with its caveats the best we could do?

The short answer:
There is no nice solution to this problem.

The longer:
Basically you would need to protect the static by a mutex, i.e.:

foo()
{
   static mutex m;
   lock lk(m);
   static myobject mo();
   lk.unlock();
}

But wait, this does not work, since boost mutex also is not currently
statically initializeable. So the only thing you currently can do is the
following ugly solution:

myobject& getmo()
{
     static myobject mo();
     return mo;
}

foo()
{
    static int flag;
    run_once(&flag, getmo);
    myobject& mo = getmo();
        
}

(Pls. don't take the above literally, it hasn't seen a compiler.)
You might have a look at the regex tests which are using this aproach.

Or you can search for the thread: "Proposal for thread safe
initialization of local static and global objects". However we did not
adopt this becasue it does not solve all problems, but it might be
enough for you.

Roland


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