Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] safe portable static mutex initialization
From: Serge Skorokhodov (ucayalyfish_at_[hidden])
Date: 2012-04-19 02:42:24


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?

TIA

-- 
The fish that is singing in Ucayali river...

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