473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

What is the problem with writing singleton in multithreaded enviroment

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?

Jan 14 '07 #1
3 6302
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?
Jan 14 '07 #2
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?
Jan 14 '07 #3
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::once_flag once_flag;
static T const & get_instance()
{
static T theT;
return theT;
}

static void call_once()
{
get_instance();
}

};

Jan 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: [Mystic] | last post by:
I was just wondering something about this. I wanted to do something similar, like having a mysql class that there can only be one of. The purpose of this was not to have hundereds to mysql...
16
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
8
by: Robert Zurer | last post by:
I have a server application that makes a MarshalByReferenceObject available via remoting. It's lifetime is set to never expire and it is implemented as a Singleton. Are calls to this object...
11
by: Daniel Billingsley | last post by:
Let's say I'm writing a business app and I want there to be only one instance of the Customer object for each particular customer (representing a database record) being edited. Would it be...
5
by: Ajay Pal Singh | last post by:
Hi, I've one question related to singelton implementation of a class. Suppose a class is as Singelton and contains a public method. If there are more than one requests for the same method...
8
by: Gaensh | last post by:
HI, I have a singleton pattern to acess my database the following is the sample code use to implement singleton pattern public class DataAccessHelper { private static DataAccessHelper instance;...
1
by: Francois Malgreve | last post by:
Hi, I am implemeting a singleton object in my ASP.NET application. Therefore I need to know if ASP.NET is multithreaded or not. All i know is that there is one worker process running ASP.NET...
24
by: ypjofficial | last post by:
Hello all, I have written a class with many private data members.and i am putting it in a separate dll file. Now when i link that file while writing my main program module,natuarally i have to...
9
by: George | last post by:
I am trying to implement postponed initialization (object is not initialize till requested). public class clsStore { volatile List<clsPictureGroup_lstPictureGroups = null; public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.