Hello,

On Fri, Apr 20, 2012 at 8:04 AM, Juan Ramírez <juanantonio.ram@gmail.com> 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@wanadoo.fr> 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@lists.boost.org
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users