thread-safe initialization for singleton

Hi, I have written a class with static methods and properties. Now the class must be used in a multithreaded environment, so I get some race conditions error. I use boost::thread for implementating the threads. I have found boost_once but I don't know how to use it, so my class properties are initialized once. Thanks Phil

On Sat, 2010-08-21 at 15:58 +0300, Kraus Philipp wrote:
http://www.google.com/search?q=boost_once+singleton --> Mika Heiskanen

Am 22.08.2010 um 00:12 schrieb OvermindDL1:
The first example? I need only to implementate the boost::noncopyable and the this strucutres template<class T> boost::scoped_ptr<T> Templates::Singleton<T>::t(0); template<class T> boost::once_flag Templates::Singleton<T>::flag = BOOST_ONCE_INIT; ? I've found many example that descripe more code like this http://blog.defx.de/2008/12/07/singletons-und-thread-locks-via-boost/ !? Thanks

On 2010-08-22 00:12:39 +0200, OvermindDL1 said:
The first example http://www.boostcookbook.com/Recipe:/1235044 don't have static members, because myclass is a class with is a master class of the Singleton. In my class I've got a static property of the boost mersenne twister and get the rase condition error on it's get method: boost::detail::backward_compatible_uniform_01<boost::random::detail::pass_through_engine<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&>, double>::operator()()

Le 22/08/2010 07:59, Philipp Kraus wrote:
The first example http://www.boostcookbook.com/Recipe:/1235044 don't have static members,
That doesn't change anything. A singleton with static members is kind of pointless though.
because myclass is a class with is a master class of the Singleton.
What is that supposed to mean?
I don't understand what this is supposed to mean either. Why not attach a testcase showing the problem?

Am 22.08.2010 um 11:41 schrieb Mathias Gaunard:
I have written my own random class, in which I use a static boost::mt19937. On a single thread always works fine, but on multithread I get race condition errors. So I would like to create a static member over all threads. My question should be "how can I create a static member over multithreads". During searching the web and reading articles, I think it's a bad idea to do that. I have re- written my class and change the static member to a non-static member. Now every thread gets his own object and I don't run into race conditions Thx Phil

On Sun, Aug 22, 2010 at 11:00 AM, Kraus Philipp <philipp.kraus@flashpixx.de> wrote:
In this case, could you not have had a shared pointer (on the heap) that points to your object, you can then set up appropriate mutex's. I would imagine it depends on the size of the object, so multiple members verses wait conditions for locks is perhaps the consideration (could the object be smaller). I would have thought initialising the member instead as a variable (if it's just an rng, don't put it in the object) in a namespace with a header guard would work.
participants (6)
-
David Irvine
-
Kraus Philipp
-
Mathias Gaunard
-
Mika Heiskanen
-
OvermindDL1
-
Philipp Kraus