Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] safe portable static mutex initialization
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-04-19 13:48:50


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