Singleton and Local static object:

A

Alamelu

Is the below code thread safe, in multithreaded environments will a race
condition happen as we are using static

Singleton* Singleton::Instance ()
{
static Singleton s;
return &s;
}

Regards,
Alamelu
 
B

Brian Muth

That particular code segment is thread-safe. However, if you want to actually update the contents of the Singleton class, you need
to make sure you write that code in a thread-safe manner.
 
B

Ben Voigt [C++ MVP]

C++ makes no guarantees in multithreaded environments. So the
initialization of a static object could easily have a race condition. C++0x
will address parallel execution for the first time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top