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