U
Udit Sharma
Hi,
I was informed by my colleague that the following code is not thread
safe but I still don't know why because his arguments weren't very
convincing.
//Singleton.h
class Singleton
{
private:
static Singleton instance;
Singleton(){}
Singleton(const Singleton&){}
Singleton& operator=(Singleton const&){}
public:
static Singleton& getInstance();
};
//Singleton.cpp
#include "Singleton.h"
Singleton Singleton::instance;
Singleton& Singleton::getInstance()
{
return instance;
}
Since am maintaining a static instance this should be created only
once irrespective of how many threads are accessing the getInstance()
function at the same time. So is this code not thread safe?
Cheers
I was informed by my colleague that the following code is not thread
safe but I still don't know why because his arguments weren't very
convincing.
//Singleton.h
class Singleton
{
private:
static Singleton instance;
Singleton(){}
Singleton(const Singleton&){}
Singleton& operator=(Singleton const&){}
public:
static Singleton& getInstance();
};
//Singleton.cpp
#include "Singleton.h"
Singleton Singleton::instance;
Singleton& Singleton::getInstance()
{
return instance;
}
Since am maintaining a static instance this should be created only
once irrespective of how many threads are accessing the getInstance()
function at the same time. So is this code not thread safe?
Cheers