-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1.cpp
More file actions
185 lines (145 loc) · 4.17 KB
/
Copy pathproblem1.cpp
File metadata and controls
185 lines (145 loc) · 4.17 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
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 500000;
const int NUM_THREADS = 12;
const int MIN = numeric_limits<int>::min();
const int MAX = numeric_limits<int>::max();
// these are for the ease of giving out random gifts to
// insert and delete from the linked list
vector<thread> servants;
atomic<int> counter = 0;
// create some ordering of the operations so that it makes the simulating easier
vector<int> order(N);
atomic<int> orderFront, orderBack;
// linked list node with value, next, and a mutex
struct node {
int value;
node *next;
mutex m;
node(int value): value(value) {}
};
class LockOrderedLinkedList {
private:
node *head;
public:
LockOrderedLinkedList() {
// make dummy nodes so that there are always 2 nodes to insert the new node in between
head = new node(MIN);
head->next = new node(MAX);
}
void insert(int value) {
// create the new node
node *curr = new node(value);
// get the head and lock it
node *prev = head;
unique_lock<mutex> prevLock(prev->m);
// get the next node and lock it
node *next = prev->next;
unique_lock<mutex> nextLock(next->m);
// while the next node is smaller than what we are adding continue going along
while(value > next->value) {
prev = next;
prevLock.swap(nextLock);
next = next->next;
nextLock = unique_lock<mutex>(next->m);
}
// add the node into the linked list
prev->next = curr;
curr->next = next;
}
void remove(int value) {
// get the head and lock it
node *prev = head;
unique_lock<mutex> prevLock(prev->m);
// get the next node and lock it
node *curr = prev->next;
unique_lock<mutex> nodeLock(curr->m);
// get the next node and lock it
node *next = curr->next;
unique_lock<mutex> nextLock(next->m);
// while the curr node is smaller than what we are trying to remove continue going along
while(value > curr->value) {
prev = curr;
prevLock.swap(nodeLock);
curr = next;
nodeLock.swap(nextLock);
// if the curr node is MAX, node doesn't exist, return
if(curr->value == MAX) {
return;
}
next = next->next;
nextLock = unique_lock<mutex>(next->m);
}
// if the current node is the value, remove it, otherwise, do nothing
if(curr->value == value) {
prev->next = next;
}
}
bool search(int value) {
//get the head and lock it
node *node = head;
unique_lock<mutex> nodeLock(node->m);
// while the value is smaller than what we are trying to search for continue going along
while(value < node->value) {
node = node->next;
auto nextLock = unique_lock<mutex>(node->m);
nodeLock.swap(nextLock);
}
// return whether the node we got is the correct value
return node->value == value;
}
};
LockOrderedLinkedList linkedList;
void servant(int id) {
int operation = 0;
// while there are nodes to remove, continue going along
while(orderFront < N) {
// insert
if(operation == 0) {
if(orderBack == N) continue;
int pos = orderBack++;
int value = order[pos];
linkedList.insert(value);
counter++;
// flip which operation this servant will do
operation ^= 1;
continue;
}
// remove
if(operation == 1) {
if(orderFront == orderBack) continue;
int pos = orderFront++;
int value = order[pos];
linkedList.remove(value);
// flip which operation this servant will do
operation ^= 1;
continue;
}
}
}
int main() {
// seed our random number generator
srand(time(0));
for(int i = 0; i < N; i++) {
order[i] = i + 1;
}
for(int i = 0; i < 10000000; i++) {
int a = rand() % N;
int b = rand() % N;
swap(order[a], order[b]);
}
linkedList = LockOrderedLinkedList();
for(int i = 0; i < NUM_THREADS; i++) {
servants.push_back(thread(servant, i));
}
for(int i = 5000; i <= 500000; i += 5000) {
while(counter < i);
cout << ((i * 100) / 500000) << "%" << endl;
}
for(int i = 0; i < NUM_THREADS; i++) {
servants[i].join();
}
cout << "All gifts have been put on the list and removed." << endl;
return 0;
}