Singleton and Local static object:

  • Thread starter Thread starter Alamelu
  • Start date Start date
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
 
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.
 
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.
 
Back
Top