-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingleton2.cpp
69 lines (53 loc) · 1.46 KB
/
Singleton2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
class Singleton2
{
public:
static Singleton2* getSingleton2Instance();
private:
static Singleton2* _mInstance;
static std::once_flag _mOnceFlag;
//To check how many times Singleton2 constructor is called.
static int _mCount;
Singleton2();
};
Singleton2* Singleton2::_mInstance {nullptr};
std::once_flag Singleton2::_mOnceFlag;
int Singleton2::_mCount = 0;
Singleton2* Singleton2::getSingleton2Instance()
{
//call_once() will be called only once irrespective of how many threads are running.
std::call_once(_mOnceFlag, [&]() {
if(_mInstance == nullptr){
_mInstance = new Singleton2();
}
} );
return _mInstance;
}
Singleton2::Singleton2()
{
++_mCount;
cout<< "\n Singleton2 constructor. I'm called - " << _mCount << " times." << endl;
}
void threadFunc()
{
cout<<"\n Thread-2: Calling getSingleton2Instance() 10 times: " << endl;
for(int i=0; i< 10; ++i)
{
Singleton2::getSingleton2Instance();
}
}
int main()
{
cout<<"****** Thread-safe Singleton by using std::call_once ******" << endl;
thread t(threadFunc);
cout<<"\n Thread-1: Calling getSingleton2Instance() 10 times: " << endl;
for(int i=100; i< 110; ++i)
{
Singleton2::getSingleton2Instance();
}
t.join();
return EXIT_SUCCESS;
}