473,508 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this Singleton class thread-safe?

Hello

I have written a singleton class like this:

//---------------------------------------------------------------------------
// Instance
//---------------------------------------------------------------------------
template <typename T>
T * CSingleton<T>::Instance(
) throw()
{
static T inst;
return &inst;
}

Is this thread safe? Could two threads (with cpu context switching)
possibly create more than one instance of the class passed as a
template? Is this possible? If it is I presume I must lock using
mutex or some mechanism before line - static T inst; and just after
it?

Angus

Nov 14 '07 #1
4 3188
DJ
Angus napisał(a):
Hello

I have written a singleton class like this:

//---------------------------------------------------------------------------
// Instance
//---------------------------------------------------------------------------
template <typename T>
T * CSingleton<T>::Instance(
) throw()
{
static T inst;
return &inst;
}

Is this thread safe? Could two threads (with cpu context switching)
possibly create more than one instance of the class passed as a
template? Is this possible? If it is I presume I must lock using
mutex or some mechanism before line - static T inst; and just after
it?
It is not thread safe and you must lock it, however mutex lock might
give performace penalty - depends on yours app. There is something
called double-checking locking - google for that - it's well known and
described problem
Nov 14 '07 #2
DJ wrote:
Angus napisał(a):
>Hello

I have written a singleton class like this:

//---------------------------------------------------------------------------

// Instance
//---------------------------------------------------------------------------

template <typename T>
T * CSingleton<T>::Instance(
) throw()
{
static T inst;
return &inst;
}

Is this thread safe? Could two threads (with cpu context switching)
possibly create more than one instance of the class passed as a
template? Is this possible? If it is I presume I must lock using
mutex or some mechanism before line - static T inst; and just after
it?

It is not thread safe and you must lock it, however mutex lock might
give performace penalty - depends on yours app. There is something
called double-checking locking - google for that - it's well known and
described problem

Some compilers do make sure it's thread safe (see G++ past V4).

DCL (double checked locking) is not considered a good thing because it
does not work on all machines - see past articles on
comp.programing.threads.
Nov 14 '07 #3
DJ
Gianni Mariani napisał(a):
DJ wrote:
>Angus napisał(a):
>>Hello

I have written a singleton class like this:

//---------------------------------------------------------------------------

// Instance
//---------------------------------------------------------------------------

template <typename T>
T * CSingleton<T>::Instance(
) throw()
{
static T inst;
return &inst;
}

Is this thread safe? Could two threads (with cpu context switching)
possibly create more than one instance of the class passed as a
template? Is this possible? If it is I presume I must lock using
mutex or some mechanism before line - static T inst; and just after
it?


It is not thread safe and you must lock it, however mutex lock might
give performace penalty - depends on yours app. There is something
called double-checking locking - google for that - it's well known and
described problem

Some compilers do make sure it's thread safe (see G++ past V4).
I am just thinking if you call ::Instace before threads are created in
the app you should be fine as the static object must be created,
shoudn't you ?
>
DCL (double checked locking) is not considered a good thing because it
does not work on all machines - see past articles on
comp.programing.threads.
true.
Nov 14 '07 #4
DJ wrote:
....
I am just thinking if you call ::Instace before threads are created in
the app you should be fine as the static object must be created,
shoudn't you ?
Yes.
>>
DCL (double checked locking) is not considered a good thing because it
does not work on all machines - see past articles on
comp.programing.threads.

true.
Nov 14 '07 #5

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

Similar topics

10
2205
by: Opa | last post by:
I have tried for two days to solve this problem with no luck. I have a singleton class which has some events declared. When I inherit from this class the events don't seem to come along with it....
7
2552
by: Robert W. | last post by:
I've just been reading all about the Singleton class and understand how to implement and use it but I cannot understand why one NEEDS to use it instead of just declaring a class and implementing...
15
63711
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
14
3005
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is...
2
1263
by: Michel van den Berg | last post by:
Hello, I tried to implement the GoF Singleton Pattern. What do you think? Public MustInherit Class Singleton(Of T As New) Public Sub New() If SingletonCreator.Creating = False Then Throw...
9
14124
by: Marcel Hug | last post by:
Hallo NG ! I Have a little question about inheritance of a singleton class. In my application i have a Database-Connection Lib, in which I would ¨like to connect different databases of the same...
3
2950
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
4
1459
by: Bryan | last post by:
Im new to threading and am experiencing some confusion as to how to correctly use AfxBeginThread in my singleton class. The code that I want to thread out is in a single function, Init(). Is...
3
3130
by: gzeng | last post by:
I saw a piece of code from a website. It seems to be a simple example for a singleton class. Basically, the author creates an object in the definition of a class, which has the same name as the...
2
1872
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define...
0
7225
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
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
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
5629
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,...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.