Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What is the problem with writing singleton in multithreaded enviroment

Reply
Thread Tools

What is the problem with writing singleton in multithreaded enviroment

 
 
Ronen Yacov
Guest
Posts: n/a
 
      01-14-2007
Hi everybody,

When declaring a singleton, we write:

static CMySingle::instance()
{
static CMySingle instance;
return instance;
}

Why should there be a problem in a multithreaded enviroment?
If one thread creates a static object the other threads will have the
same
instance (becuase its static).

And if there is a problem, how the docuble check locking solves it?

 
Reply With Quote
 
 
 
 
Milind
Guest
Posts: n/a
 
      01-14-2007
What problem are you anticipating?? Is there something specific that
you are looking for. We use implementation which is pretty similar to
what you have quoted and it works fine with around 40 threads running
in the system.
Kindly, give more details.

~M
Ronen Yacov wrote:
> Hi everybody,
>
> When declaring a singleton, we write:
>
> static CMySingle::instance()
> {
> static CMySingle instance;
> return instance;
> }
>
> Why should there be a problem in a multithreaded enviroment?
> If one thread creates a static object the other threads will have the
> same
> instance (becuase its static).
>
> And if there is a problem, how the docuble check locking solves it?


 
Reply With Quote
 
 
 
 
robbies@gmail.com
Guest
Posts: n/a
 
      01-14-2007
I did a little search of old group threads, and this popped up:
http://groups.google.com/group/comp....1b524310a37e9e

The jist of it is, apparently, as the thread starter discovered, the
static construction of an object isn't an atomic operation. When it
gets compiled, it is turned into a set of instructions where a flag is
checked before the object is instantiated (which makes sense...I don't
know if the C++ Standard mentions whether it has to be done in the
certain way...I should look it up...). So if you think about it,
multiple threads could get past this check to instantiate the object,
which makes this not thread safe.


Ronen Yacov wrote:
> Hi everybody,
>
> When declaring a singleton, we write:
>
> static CMySingle::instance()
> {
> static CMySingle instance;
> return instance;
> }
>
> Why should there be a problem in a multithreaded enviroment?
> If one thread creates a static object the other threads will have the
> same
> instance (becuase its static).
>
> And if there is a problem, how the docuble check locking solves it?


 
Reply With Quote
 
u.int.32.t@gmail.com
Guest
Posts: n/a
 
      01-15-2007
Milind wrote:
> What problem are you anticipating?? Is there something specific that
> you are looking for. We use implementation which is pretty similar to
> what you have quoted and it works fine with around 40 threads running
> in the system.
> Kindly, give more details.


Your implementation will break on a SMP box. The correct (using
boost::call_once) is:

struct T
{
static T const & instance()
{
boost::call_once(&call_once,once_flag);
return get_instance();
}
private:
static boost:nce_flag once_flag;
static T const & get_instance()
{
static T theT;
return theT;
}

static void call_once()
{
get_instance();
}

};

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
postponed Initialization in multithreaded enviroment. George ASP .Net 9 10-27-2008 10:08 PM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
What is the problem with writing singleton in multithreaded enviroment Ronen Yacov Java 7 01-14-2007 08:23 PM
Re: cgi "print statement" in multithreaded enviroment? vegetax Python 1 05-03-2005 02:35 PM
cgi "print statement" in multithreaded enviroment? vegetax Python 5 05-03-2005 02:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57