class NumberStorage {
public:
static NumberStorage& instance();
double getNumber();
void setNumber(double d);
private:
NumberStorage() { number = 0.0; };
double number;
};
NumberStorage& NumberStorage::instance()
{
static NumberStorage instance;
return instance;
}
I think I have read somewhere that the instance() method implemented this way is thread safe. Is this correct? I guess I also must lock the member variable number in the getNumber() and setNumber()? How do I do this (C++11)?
Simple lock can be used with
std::unique_lock
andstd::mutex
: