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.
> 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.:

   static mutex m;
   lock lk(m);
   static myobject mo();

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;

    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.


Boost list run by bdawes at, gregod at, cpdaniel at, john at