Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] safe portable static mutex initialization
From: Will Mason (willchido_at_[hidden])
Date: 2012-04-20 09:30:02


Hello,

On Fri, Apr 20, 2012 at 8:04 AM, Juan Ramírez <juanantonio.ram_at_[hidden]>wrote:

> It's not related to the user type, neither to mutex initialization,
> it's related to variable initialization in general.
>
> A global static variable is guaranteed to be initialized before usage.
>
> But the initialization of static variables declared inside a function
> is not guaranteed to be thread safe. So you could easily end up
> calling it's constructor twice.
>

That's something that boost::call_once is good at preventing.

Will

>
>
> Saludos!
> Juan
>
>
>
> On Thu, Apr 19, 2012 at 2:48 PM, Vicente J. Botet Escriba
> <vicente.botet_at_[hidden]> wrote:
> > Le 19/04/12 08:42, Serge Skorokhodov a écrit :
> >
> >> Hi,
> >>
> >>>> 1. Is it safe to use a block level static boost::mutex on Unix and
> >>>> Windows?
> >>>> 2. Is it safe to use a file/class level static boost::mutex on Unix
> and
> >>>> Windows?
> >>>
> >>> I don't see what kind of problems are your expecting. Could you
> clarify?
> >>> What do you mean by safe?
> >>>
> >> Say, I need a threadsafe static factory method. It may be implemented
> >> in two ways:
> >>
> >> 1.
> >> class F
> >> {
> >> ...
> >> public:
> >> static PTR_TO_SOMETHING* create_something()
> >> {
> >> static boost::mutex m;
> >> boost::scope_lock(m);
> >> ....
> >> retrunt something_created;
> >> }
> >> };
> >>
> >> 2.
> >> H-file:
> >> class F
> >> {
> >> private:
> >> static boost::mutex m_;
> >> ...
> >> public:
> >> static PTR_TO_SOMETHING* create_something();
> >> };
> >> CPP-file:
> >> boost::mutex F:m_;
> >>
> >> PTR_TO_SOMETHING* F::create_something()
> >> {
> >> boost::scope_lock(m_);
> >> ....
> >> retrunt something_created;
> >> }
> >>
> >> create_something is to be called from different threads but after main
> >> is entered. Are both of the above implementation threadsafe?
> >>
> > How this is issue is related to mutex initialization? Isn't this
> independent
> > from the UDT?
> >
> > Best,
> > Vicente
> >
> >
> > _______________________________________________
> > Boost-users mailing list
> > Boost-users_at_[hidden]
> > http://lists.boost.org/mailman/listinfo.cgi/boost-users
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users



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