473,387 Members | 1,379 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,387 software developers and data experts.

singleton with multithreading

ank
Hi,
I have some question about Singleton Pattern in C++.

I think I understand that static initialization order across
translation unit
is unspecified by the standard, but what can I do to ensure that some
kind of
static mutex is automatically initialized before the first use of it?

I cannot assume that this mutex is used only after executing main()
function because I need to use this mutex to lock singleton inside
initialization code and may apply double-check pattern.

Currently, I have very little experience with multithread programming.
I am not a native English speaker so my message may not be well
written.

Thanks in advance for any suggestions.

Jul 23 '05 #1
6 3026
On Sun, 26 Jun 2005 13:00:41 +0400, ank <ek*******************@gmail.com>
wrote:
Hi,
I have some question about Singleton Pattern in C++.

I think I understand that static initialization order across
translation unit
is unspecified by the standard, but what can I do to ensure that some
kind of
static mutex is automatically initialized before the first use of it?


To force initialization without dependency on module initialization order
you may employ schwarz counter idiom (google groups for it). This is how
standard streams (std::cout, etc) get initialized.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #2
ank
Which means I have to include the initialization code in every module
that
use my service.
Am I right?

Jul 23 '05 #3
On Sun, 26 Jun 2005 13:26:30 +0400, ank <ek*******************@gmail.com>
wrote:
Which means I have to include the initialization code in every module
that use my service.
Am I right?


Only the code that calls your initialization code at the proper moment.
The initialization code can be in any translation unit you like.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #4
ank
Maxim Yegorushkin wrote:

Only the code that calls your initialization code at the proper moment.
The initialization code can be in any translation unit you like.

--
Maxim Yegorushkin
<fi****************@gmail.com>


Thanks for your suggestion Maxim, I think it is the reasonable thing to
do to deal with the problem.

I think I can use the counter to help initialize mutex before any code
that acquire lock with it and all is done, in thread-safe fashion.
This technique don't suffer from threading issue because you have no
way use the object before the counter is initialized, by any
global/static external object, in any thread, right?.

Is Schwarz counter efficient enough
or is it best to use non-portable compiler option?
I want to know other's opinion.

Jul 23 '05 #5
On Sun, 26 Jun 2005 14:31:38 +0400, ank <ek*******************@gmail.com>
wrote:
Maxim Yegorushkin wrote:

Only the code that calls your initialization code at the proper moment.
The initialization code can be in any translation unit you like.

[]
I think I can use the counter to help initialize mutex before any code
that acquire lock with it and all is done, in thread-safe fashion.
This technique don't suffer from threading issue because you have no
way use the object before the counter is initialized, by any
global/static external object, in any thread, right?.


Right. By the time you use it it has already been initialized.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #6
ank wrote:
Hi,
I have some question about Singleton Pattern in C++.

I think I understand that static initialization order across
translation unit
is unspecified by the standard, but what can I do to ensure that some
kind of
static mutex is automatically initialized before the first use of it?

I cannot assume that this mutex is used only after executing main()
function because I need to use this mutex to lock singleton inside
initialization code and may apply double-check pattern.

Currently, I have very little experience with multithread programming.
I am not a native English speaker so my message may not be well
written.

Thanks in advance for any suggestions.


Well for starts, read this
http://www.cs.umd.edu/~pugh/java/mem...edLocking.html
This may kick off one of those interminable discussions where about half
of the participants will never get it.

To summarize, DCL isn't actually broken, it just that there aren't any
portable mechanisms to implement it correctly on all platforms. The
naive implementation using pointers assumes dependent load ordering. You
have to know which platforms this works on. It doesn't work on Alpha
processors.

There are design patterns let you implement DCL correctly. One is to have
each thread use a thread local variable whether it has a properly synchronized
view of the singleton data. The other is to only access the singleton via
a object of that class which has been properly synchronized with the singleton
in its construction. Access to the objects is assumed to be properly synchronized
also.

Initialization of a Posix pthread mutex is
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

For windows you can use a named Mutex which can be dynamically initialized in
a thread-safe manner. Windows CriticalSection initialization isn't thread-safe.
You can use compare and swap to initialize a CriticalSection. Example of this
here in the initFreeQueue subroutine. It also uses the access via object technique.
http://groups.google.com/groups?q=g:...6%40xemaps.com
I'd stay away of any naive spinlock implementations. Not only are they inefficient but
they're likely to be incorrect with regard to proper memory visibility, i.e. missing
necessary memory barriers.


--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Jul 23 '05 #7

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

Similar topics

18
by: Alfonso Morra | last post by:
Hi, Anyone knows of a link to a singleton design pattern example in C++ (preferably not using a global variable?).
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...
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...
15
by: Nick Keighley | last post by:
Hi, I found this in code I was maintaining template <class SingletonClass> SingletonClass* Singleton<SingletonClass>::instance () { static SingletonClass _instance; return &_instance; }
2
by: keepyourstupidspam | last post by:
Hi, I am wondering if My Singleton class is ok from a thread safety point of view. Am i adding the thread locks in the right places? template <typename T> class DLLEXPORT CSingleton
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
1
by: tlann | last post by:
I'm getting errors about a singleton when I put it into a dll. The dll compiles without any problems. However, when I try to link it to another dll I get the following error. libFoo-Bar error...
3
by: Bob Doe | last post by:
Hello, how to I replace singleton classes using function scope static variables with one that doesn't use function scope static variables?: class Foo { public: static Foo &instance();...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.