-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingletonMT.cpp
112 lines (85 loc) · 2.14 KB
/
SingletonMT.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <memory>
#include<iostream>
#include <thread>
#include <functional>
#include <algorithm>
#include<vector>
#include<cstdatomic>
// convert gcc to c0xx
#define thread_local __thread
typedef std::thread Thread;
typedef std::vector<std::thread> ThreadGroup;
typedef std::mutex Mutex;
typedef std::unique_lock<std::mutex> Guard;
typedef std::condition_variable Condition;
template <typename T>
class Singleton {
private:
typedef Singleton<T> self;
mutable std::once_flag m_flag;
mutable std::unique_ptr<T> m_data;
std::atomic<bool> ready;
Singleton() : ready(false){
init();
ready=true;
}
void init() const {
m_data.reset(new T);
}
public:
T const & data() const {
// std::call_once(m_flag,&self::init,this);
return *m_data;
}
public:
static self & instance() {
static self me;
// while (!me.ready) nanosleep(0,0);
return me;
}
};
struct Int {
static std::atomic<int> count;
Int() : me(4){count++;}
~Int() { std::cout << "bye" << std::endl;}
int me;
};
std::atomic<int> Int::count(0);
struct Client {
static std::atomic<int> start;
static std::atomic<int> error;
~Client() {
// std::cout << "destr " << d << " " << (&(Singleton<Int>::instance().data().me)) << std::endl;
if(d!=(&(Singleton<Int>::instance().data().me))) ++error;
}
static void wait() {
--start;
do{}while(start);
}
void operator()() {
// wait everybody is ready;
wait();
d = (&(Singleton<Int>::instance().data().me));
// std::cout << "op " << d << " " << (&(Singleton<Int>::instance().data().me)) << std::endl;
}
int const * d;
};
std::atomic<int> Client::start(0);
std::atomic<int> Client::error(0);
int main() {
int NUMTHREADS = 8;
Client::start=NUMTHREADS+1;
ThreadGroup threads;
threads.reserve(NUMTHREADS);
for (int i=0; i<NUMTHREADS; ++i) {
threads.push_back(Thread(Client()));
}
Client::error=0;
std::cout << Client::start << std::endl;
Client::wait();
std::for_each(threads.begin(),threads.end(),
std::bind(&Thread::join,std::placeholders::_1));
std::cout << Int::count << std::endl;
std::cout << Client::error << std::endl;
return 0;
}