Skip to content

Instantly share code, notes, and snippets.

@slwu89
Created January 13, 2018 06:42
Show Gist options
  • Save slwu89/6aaff97e19d02067913dafd64be00020 to your computer and use it in GitHub Desktop.
Save slwu89/6aaff97e19d02067913dafd64be00020 to your computer and use it in GitHub Desktop.
thread safe c++11 singleton
#include <iostream>
using namespace std;
class CSingleton final
{
public:
static CSingleton* GetInstance();
void set_x(const int& x_){x = x_;};
int get_x(){return x;};
private:
int x;
CSingleton() = default;
~CSingleton() = default;
CSingleton(const CSingleton&) = delete;
CSingleton& operator=(const CSingleton&) = delete;
CSingleton(CSingleton&&) = delete;
CSingleton& operator=(CSingleton&&) = delete;
};
CSingleton* CSingleton::GetInstance()
{
static CSingleton instance;
return &instance;
}
int main()
{
cout<<"Hello World\n";
CSingleton::GetInstance()->set_x(5);
std::cout << CSingleton::GetInstance()->get_x() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment