-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton_pattern.cpp
52 lines (41 loc) · 1.85 KB
/
singleton_pattern.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
#include <thread>
#include <chrono>
#include <iostream>
#include <future>
constexpr auto tenMill= 10000000;
class MySingleton{
public:
static MySingleton& getInstance(){
static MySingleton instance; // As compilation is happending through C++11 , it will get initialised when the first time the decleraion is called
return instance; // returning as reference
}
/*
According to C++11 , the static valiable confirms that , valiable is initiated once .
It cam be called from multiple thread , so its developer responsibility to ensure that ctor is implemented in thread-safe way .
i.e. If there is any resource are in teh calsss , then creation of those resource needs to to synchronise accordingly .
*/
private:
// Making this implicit functions private . It can't be called from out side of class
MySingleton()= default; // using the default constructor
~MySingleton()= default; // using the default dtor
MySingleton(const MySingleton&)= delete;//deleting copy constructor
MySingleton(const MySingleton&&)= delete;//deleting move constructor
MySingleton& operator=(const MySingleton&)= delete;// deleting copy assignment
MySingleton& operator=(const MySingleton&&)= delete;// deleting move assignment
};
// we are calling this function from different thread
std::chrono::duration<double> getTime(){
auto begin= std::chrono::system_clock::now();
for ( size_t i= 0; i <= tenMill; ++i){
MySingleton::getInstance();
}
return std::chrono::system_clock::now() - begin;
};
int main(){
auto fut1= std::async(std::launch::async,getTime);
auto fut2= std::async(std::launch::async,getTime);
auto fut3= std::async(std::launch::async,getTime);
auto fut4= std::async(std::launch::async,getTime);
auto total= fut1.get() + fut2.get() + fut3.get() + fut4.get();
std::cout << total.count() << std::endl;
}