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:
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.
http://www.google.com/search?q=boost_once+singleton --> Mika Heiskanen

Am 21.08.2010 um 17:59 schrieb Mika Heiskanen:
On Sat, 2010-08-21 at 15:58 +0300, Kraus Philipp wrote:
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.
I know Google and so I've found boost_once, but can you show me a little example? Phil

On Sat, Aug 21, 2010 at 12:59 PM, Kraus Philipp <philipp.kraus@flashpixx.de> wrote:
Am 21.08.2010 um 17:59 schrieb Mika Heiskanen:
On Sat, 2010-08-21 at 15:58 +0300, Kraus Philipp wrote:
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.
I know Google and so I've found boost_once, but can you show me a little example?
He was hinting at something, look at the very first result on that Google link.

Am 22.08.2010 um 00:12 schrieb OvermindDL1:
On Sat, Aug 21, 2010 at 12:59 PM, Kraus Philipp <philipp.kraus@flashpixx.de> wrote:
Am 21.08.2010 um 17:59 schrieb Mika Heiskanen:
On Sat, 2010-08-21 at 15:58 +0300, Kraus Philipp wrote:
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.
I know Google and so I've found boost_once, but can you show me a little example?
He was hinting at something, look at the very first result on that Google link.
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 Sun, 2010-08-22 at 09:33 +0300, Kraus Philipp wrote:
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.
I know Google and so I've found boost_once, but can you show me a little example?
He was hinting at something, look at the very first result on that Google link. 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/ !?
Your link does not use boost_once, and you specifically asked for a boost_once example on how to implement a singleton. --> Mika Heiskanen

On 2010-08-22 00:12:39 +0200, OvermindDL1 said:
On Sat, Aug 21, 2010 at 12:59 PM, Kraus Philipp <philipp.kraus@flashpixx.de> wrote:
Am 21.08.2010 um 17:59 schrieb Mika Heiskanen:
On Sat, 2010-08-21 at 15:58 +0300, Kraus Philipp wrote:
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.
I know Google and so I've found boost_once, but can you show me a little example?
He was hinting at something, look at the very first result on that Google link.
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?
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()()
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:
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?
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()()
I don't understand what this is supposed to mean either. Why not attach a testcase showing the problem?
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:
Am 22.08.2010 um 11:41 schrieb Mathias Gaunard:
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?
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()()
I don't understand what this is supposed to mean either. Why not attach a testcase showing the problem?
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 _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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.

Le 22/08/2010 11:00, Kraus Philipp a écrit :
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.
static data members are initialized before main. Your problem is not concurrent initialization, it's concurrent access to the same object. It has nothing to do whatsoever with only initializing a singleton once.
participants (6)
-
David Irvine
-
Kraus Philipp
-
Mathias Gaunard
-
Mika Heiskanen
-
OvermindDL1
-
Philipp Kraus