Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safe singleton implementation in C++

The following is a well known implementation of singleton pattern in C++.
However, I'm not entirely sure whether its thread-safe.
Based upon answers to similar question asked here previously, it seems it is thread safe.
Is that so?

//Curiously Recurring Template Pattern    
//Separates a class from its Singleton-ness (almost).    
#include <iostream>  
using namespace std;

template<class T> class Singleton {
  Singleton(const Singleton&);
  Singleton& operator=(const Singleton&);
protected:
  Singleton() {}
  virtual ~Singleton() {}
public:
  static T& instance() {
    static T theInstance;
    return theInstance;
  }
};

// A sample class to be made into a Singleton
class MyClass : public Singleton<MyClass> {
 int x;
protected:
  friend class Singleton<MyClass>;
  MyClass() { x = 0; }
public:
 void setValue(int n) { x = n; }
  int getValue() const { return x; }
};
like image 287
TL36 Avatar asked Jun 27 '09 12:06

TL36


People also ask

How a singleton implementation can be thread-safe?

Thread Safe Singleton in JavaCreate the private constructor to avoid any new object creation with new operator. Declare a private static instance of the same class. Provide a public static method that will return the singleton class instance variable.

Is singleton is thread-safe or not?

Is singleton thread safe? A singleton class itself is not thread safe. Multiple threads can access the singleton same time and create multiple objects, violating the singleton concept. The singleton may also return a reference to a partially initialized object.

Which implementation of singleton pattern is best?

The most popular approach is to implement a Singleton by creating a regular class and making sure it has: A private constructor. A static field containing its only instance.

How Meyers singleton is thread-safe?

The beauty of the Meyers Singleton in C++11 is that it's automatically thread-safe. That is guaranteed by the standard: Static variables with block scope. The Meyers Singleton is a static variable with block scope, so we are done. It's still left to rewrite the program for four threads.


2 Answers

No, this is not thread safe because the static local is not guarded in any way. By default a static local is not thread safe. This means you could run into the following issues

  • Constructor for the singleton runs more than once
  • The assignment to the static is not guaranteed to be atomic hence you could see a partial assignment in multi-threaded scenarios
  • Probably a few more that I'm missing.

Here is a detailed blog entry by Raymond Chen on why C++ statics are not thread safe by default.

  • http://blogs.msdn.com/oldnewthing/archive/2004/03/08/85901.aspx
like image 181
JaredPar Avatar answered Sep 25 '22 07:09

JaredPar


IT IS NOT THREAD SAFE. To become thread safe you should add a check before the lock (semaphore lock) and an other check after the lock. And then you are sure that even in simultaneous call from different threads you provide one instance.

like image 38
Makram Ghazzaoui Avatar answered Sep 25 '22 07:09

Makram Ghazzaoui