Boost logo

Boost Users :

From: Christopher Hunt (huntc_at_[hidden])
Date: 2005-08-17 15:22:20


On 18/08/2005, at 1:19 AM, boost-users-request_at_[hidden] wrote:

> I envisage two threads accessing a function like this concurrently:
>
> template<typename T>
> boost::shared_ptr<T>
> my_func()
> {
> static boost::mutex m;
> boost::mutex::scoped_lock l(m);
>
> static boost::shared_ptr<T> p(new T);
> return p;
> }
>
> and my worry is that both threads could attempt to do the static
> initialisation of m concurrently. Is there protection against this?
> Is it even possible to protect against this?
The scope of m is actually outside of your function given that it is
static. Thus it will be initialised outside of your function generally
before any other threads get to start up.

Cheers,
-C


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net