-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMRMW-updated.cpp
218 lines (165 loc) · 3.64 KB
/
MRMW-updated.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <fstream>
#include <thread>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//using namespace std;
//atomic_int** HELPSNAP;
//int num, lambda1 ,lambda2 ,k;
atomic_bool term;
// this is a sample code
class MRMWSnap
{
// public:
// atomic_int value;
// int pid;
// int seq;
// MRMWSnap()
// {
// value.store(-1, memory_order_relaxed);
// pid = -1;
// seq = 0;
// }
// void update_val(int id ,int val)
// {
// seq++;
// pid = id;
// value.store( val , std::memory_order_relaxed);
// HELPSNAP[id] = snap();
// }
// atomic_int snap()
// {
// set<int> can_help;
// MRMWSnap aa[num];
MRMWSnap bb[num];
// for(int i = 0 ;i < num ;i++)
// {
// //aa[i] = reg[i];
// (aa[i].value).store(reg[i].value , memory_order_relaxed);
// aa[i].seq = reg[i].seq;
// aa[i].pid = reg[i].pid;
// }
while(1)
{
for(int i = 0 ;i < num ;i++)
{
//(bb[i].value).store(reg[i].value , memory_order_relaxed);
bb[i].seq = reg[i].seq;
bb[i].pid = reg[i].pid;
}
int count = 0;
for(int i = 0 ;i < num ;i++)
{
if(aa[i].value == bb[i].value)
{
count++;
}
}
if(count != num)
{
atomic_int ans[num];
for (int i = 0; i < num ; i++)
{
//ans[i].store(bb[i].value , memory_order_relaxed);
}
return ans;
}
for(int i = 0 ;i < num ;i++)
{
if(aa[i].value != bb[i].value)
{
const bool is_in = can_help.find(i) != can_help.end();
if(is_in)
{
return HELPSNAP[i];
}
else
{
can_help.insert(i);
}
}
}
for(int i = 0 ;i < num ;i++)
{
(aa[i].value).store(bb[i].value , memory_order_relaxed);
aa[i].seq = bb[i].seq;
aa[i].pid = bb[i].pid;
}
}
}
};
MRMWSnap* reg;
MRMWSnap** HELPSNAP;
double ran_exp(float lambda) //finds the ranom exponential time for slaeping the thread
{
default_random_engine generate;
srand(5);
exponential_distribution <double> distribution(1/lambda);
return distribution (generate);
}
void thWrite(int i)
{ int number;
time_t exitTime;
srand(time(NULL));
while(!term)
{
number = i+rand()%100;
reg[i].update_val(i,number);
exitTime = time(0);
tm* ltm = localtime(&exitTime);
printf("Thread%d writes %d at %d:%d\n", i+1 ,number,1 + ltm->tm_min,1 + ltm->tm_sec);
usleep(100000*ran_exp(lambda1));
}
}
void getSnap()
{
int i = 0;
time_t exitTime;
while(i < k)
{
auto start = std::chrono::system_clock::now();
atomic_int* res = cls->scan();
auto end = std::chrono::system_clock::now();
exitTime = time(0);
string s = "Snapshot: ";
for (int j = 0; j < num ; j++)
{ int val = res[j].load(std::memory_order_relaxed);
s+= "th"+ to_string(j+1) + "-"+to_string(val);
}
tm* ltm = localtime(&exitTime);
s+= "finished at" + to_string(1+ltm->tm_min) +":" + to_string(1+ltm->tm_sec);
printf("%s\n", s.c_str());
std::chrono::duration<double> elapsed_seconds = end - start;
avg_time += elapsed_seconds.count();
usleep(100000*ran_exp(lambda2));
i++;
}
term.store(true , std::memory_order_relaxed);
}
int main()
{
cout<<"enter the params"<<endl;
cin>>num>>lambda1>>lambda2>>k ;
term.store(false,std::memory_order_relaxed);
reg = new MRMWSnap[num];
HELPSNAP = new MRMWSnap*[num];
for(int i = 0; i < num ; i++)
{
HELPSNAP[i] = reg;
}
thread writers[num];
for(int i=0 ;i<num ; i++)
{
writers[i] = thread(thWrite ,i);
}
// thread snappy = thread(getSnap);
// snappy.join();
// for(int i=0 ;i<num ; i++)
// {
// writers[i].join();
// }
// //delete cls;
// return 0;
// }