-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.cpp
More file actions
66 lines (55 loc) · 1.65 KB
/
Singleton.cpp
File metadata and controls
66 lines (55 loc) · 1.65 KB
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 <mutex>
#include <thread>
#include <iostream>
#include <chrono>
#include <string>
class Singleton
{
private:
static Singleton* pinstance_;
static std::mutex mutex_;
protected:
Singleton(const std::string value) : value_(value) {}
~Singleton() {}
std::string value_;
public:
Singleton(Singleton &other) = delete; // block cloning
void operator=(const Singleton& other) = delete; // block assignment
static Singleton *getInstance(const std::string& value); // requires thread handing to avoid copying
void functionalLogic()
{
// ....
}
std::string value() const {
return value_; // oh that's why we have underscore - for the function name...
}
};
Singleton* Singleton::pinstance_ = nullptr;
std::mutex Singleton::mutex_;
Singleton *Singleton::getInstance(const std::string& value) {
std::lock_guard<std::mutex> lock(mutex_); // prevents race conditions
if(pinstance_ == nullptr) {
pinstance_ = new Singleton(value);
}
return pinstance_;
}
void threadFoo() {
// initialize one instance
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
Singleton* singleton = Singleton::getInstance("foo");
std::cout << singleton->value() << "\n";
}
void threadBar() {
// initialize another instance
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
Singleton* singleton = Singleton::getInstance("foo");
std::cout << singleton->value() << "\n";
}
int main() {
std::cout << "If you see different values, something went wrong\n";
std::thread t1(threadFoo);
std::thread t2(threadFoo);
t1.join();
t2.join();
return 0;
}