-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1.cpp
More file actions
212 lines (176 loc) · 4.69 KB
/
problem1.cpp
File metadata and controls
212 lines (176 loc) · 4.69 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
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
// David Huynh
// COP 4520, Spring 2024
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <shared_mutex>
#include <random>
#include <numeric>
#include <algorithm>
// Number of presents.
#define NUMP 500000
// Linked list node, where every node has a mutex.
struct Node
{
std::shared_mutex mtx;
int val;
Node *next;
Node(int n) : val(n), next(nullptr) {}
};
class PresentRoom
{
private:
std::shared_mutex mutex_;
Node *head;
Node *tail;
std::vector<int> presentBag;
int cardCount;
public:
// Default constructor that creates an empty head/tail for the linked list and generates the shuffled present bag.
PresentRoom() : presentBag(NUMP), cardCount(0)
{
head = new Node(-1);
tail = new Node(500001);
head->next = tail;
std::iota (std::begin(presentBag), std::end(presentBag), 1);
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
shuffle(presentBag.begin(), presentBag.end(), std::default_random_engine(seed));
}
void addPresent();
void removePresent();
void findPresent();
int getCardCount();
};
// Adds a present to the chain of presents.
void PresentRoom::addPresent()
{
int num = -1;
// Grabs a random present from the bag to add.
std::unique_lock lock(mutex_);
if (!presentBag.empty())
{
num = presentBag.front();
presentBag.erase(presentBag.begin());
}
else
{
return;
}
lock.unlock();
// Locks two nodes while traversing (hand-over-hand locking).
head->mtx.lock();
Node *prev = head;
Node *curr = head->next;
curr->mtx.lock();
while (num > curr->val)
{
prev->mtx.unlock();
prev = curr;
curr = curr->next;
curr->mtx.lock();
}
// Create a new node to insert and unlock nodes.
Node *newNode = new Node(num);
newNode->next = curr;
prev->next = newNode;
curr->mtx.unlock();
prev->mtx.unlock();
}
// Removes a present from the chain of presents.
void PresentRoom::removePresent()
{
// Lock the head and the first node.
head->mtx.lock();
Node *curr = head->next;
curr->mtx.lock();
// Check that the node to delete isn't tail.
if (curr == tail)
{
curr->mtx.unlock();
head->mtx.unlock();
return;
}
// Relink nodes and delete the node.
head->next = curr->next;
curr->mtx.unlock();
head->mtx.unlock();
delete(curr);
// Increment the number of cards written.
std::unique_lock lock(mutex_);
cardCount++;
if (cardCount % 100000 == 0)
{
std::cout << "Number of cards written: " << cardCount << "\n";
}
}
// Searches the list for a present.
void PresentRoom::findPresent()
{
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(1, NUMP);
int num = dist(mt);
// Traverses the list to find a node with a specific value.
Node *curr = head->next;
while (curr != tail)
{
if (num == curr->val)
{
std::cout << "Searched for and found present #" << num << ".\n";
return;
}
curr = curr->next;
}
}
// Returns the total number of cards written.
int PresentRoom::getCardCount()
{
std::shared_lock lock(mutex_);
return cardCount;
}
// Multiple servants alternate adding and removing presents.
void performTasks(PresentRoom &room)
{
int flag = true;
int calledToCheck = 0;
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(1, 4);
// Loop until the card count matches present count.
// Each servant alternates between adding a present and removing a present.
// The minotaur also randomly asks a servant to check if a present is in the chain.
while (room.getCardCount() < NUMP)
{
calledToCheck = dist(mt);
if (flag)
{
room.addPresent();
flag = false;
}
else
{
room.removePresent();
flag = true;
}
if (calledToCheck == 1)
{
room.findPresent();
}
}
}
int main(void)
{
PresentRoom myRoom;
std::cout << "The card writing process has begun! (This may take a while...)\n";
std::thread servant1(performTasks, std::ref(myRoom));
std::thread servant2(performTasks, std::ref(myRoom));
std::thread servant3(performTasks, std::ref(myRoom));
std::thread servant4(performTasks, std::ref(myRoom));
servant1.join();
servant2.join();
servant3.join();
servant4.join();
std::cout << "The servants have finished writing cards for each present!\n";
return 0;
}