-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPS5_problem2.cpp
More file actions
73 lines (51 loc) · 1.78 KB
/
PS5_problem2.cpp
File metadata and controls
73 lines (51 loc) · 1.78 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
67
68
69
70
71
72
73
#include <iostream>
using namespace std;
struct cae_t {
int value;
bool isLocked = false;
int numAttempts = 0;
};
int test_and_set(int* mem);
bool compare_and_exchange(cae_t * mem, int compare_value, int swap_value);
int main(int argc, char *argv[])
{
cae_t mem;
mem.value = 4;
int compare_value = 4;
int exchange_value = 7;
compare_and_exchange(&(mem), compare_value, exchange_value);
//break the lock
mem.isLocked = false;
compare_and_exchange(&(mem), compare_value, exchange_value);
compare_and_exchange(&(mem), compare_value, exchange_value);
}
bool test_and_set(cae_t *mem){
if (mem->isLocked) {
//force break the lock after 100 attempts
mem->numAttempts++;
if (mem->numAttempts >= 100) {
mem->isLocked = false;
cout << "Breaking lock after 100 access attempts." << endl;
}
}
return mem->isLocked;
}
bool compare_and_exchange(cae_t * mem, int compare_value, int swap_value){
cout << "-----------------------" << endl;
cout << "Starting compare_and_exchange()" << endl;
cout << "Comparing cae_t @ " << &(mem) << ": val=" << mem->value << " to " << compare_value << endl;
bool isSwapped = false;
// acquire lock (spin until the lock is released)
while(test_and_set(mem)){}
if (compare_value == mem->value){
mem->value = swap_value;
isSwapped = true;
}
//release lock (set to true)
mem->isLocked = true;
cout << "result for " << "cae_t @ " << &(mem) << ": " << (isSwapped > 0 ? "swapped" : "not swapped") << endl;
cout << "new value for " << "cae_t @ " << &(mem) << ": " << mem->value << endl;
cout << "Finished compare_and_exchange()" << endl;
cout << "-----------------------" << endl;
return isSwapped;
}