Boost logo

Boost Users :

From: James E Taylor (james_at_[hidden])
Date: 2005-08-18 05:22:25

Are you sure there are no guarantees for function scope statics?

I think the following is thread safe:

    static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
    // do something once

because the C-style PTHREAD_MUTEX_INITIALIZER doesn't involve a constructor call: the static is initialised before _any_ threads are running.

Also, constructors of global statics are not guaranteed to be single-threaded for the same reason you can't safely reference one static from the constructor of another (The Static Initialisation Order Fiasco); one constructor could create a thread that goes and uses an uninitialised static, whilst the main thread tries to initialise it.

Do you know whether the mutex ctor is thread-safe?

T. Scott Urban <scottu_at_[hidden]> wrote :

> On Thu, 2005-08-18 at 06:22 +1000, Christopher Hunt wrote:
> &gt; On 18/08/2005, at 1:19 AM, boost-users-request_at_[hidden]
> wrote:
> &gt;
> &gt; &gt; I envisage two threads accessing a function like this
> concurrently:
> &gt; &gt;
> &gt; &gt; template&lt;typename T&gt;
> &gt; &gt; boost::shared_ptr&lt;T&gt;
> &gt; &gt; my_func()
> &gt; &gt; {
> &gt; &gt; static boost::mutex m;
> &gt; &gt; boost::mutex::scoped_lock l(m);
> &gt; &gt;
> &gt; &gt; static boost::shared_ptr&lt;T&gt; p(new T);
> &gt; &gt; return p;
> &gt; &gt; }
> &gt; &gt;
> &gt; &gt; and my worry is that both threads could attempt to do the static
> &gt; &gt; initialisation of m concurrently. Is there protection against this?
> &gt; &gt; Is it even possible to protect against this?
> &gt; The scope of m is actually outside of your function given that it is
> &gt; static. Thus it will be initialised outside of your function generally
> &gt; before any other threads get to start up.
> The scope of m is in the function, not global, but it has static
> duration. Unlike a global, m will only be constructed if my_func() is
> ever called in your program.
> That should set of warning bells in you brain.
> I don't believe there are any guarantees about the thread safety of the
> initialization of function scope statics. This applies not just to the
> mutex class in question but any type. A particular compiler might make
> this usage thread safe - or maybe only guarantee initialization of basic
> types - but I don't know if it's covered by the pthread spec - and
> that's not helpful anyway to boost users, in general. And of course,
> the C++ standard is as usual silent on the matter.
> You can make the mutex global - anonymous namespace or whatever. Since
> global initializations are single threaded, you trade the threading
> problem for the loss of on-demand resource usage.
> Another way to deal with this kind of problem is to use a modified
> singleton pattern, but to make that thread safe, you need another mutex
> (unless you want to risk DCL), so seems kind of pointless for this
> instance.
> --
> t. scott urban &lt;scottu_at_[hidden]&gt;

James E Taylor

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at