-
Notifications
You must be signed in to change notification settings - Fork 55
/
philosophy01.cc
44 lines (35 loc) · 949 Bytes
/
philosophy01.cc
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
#include <cstdio>
#include <cassert>
#include <unistd.h>
#include <random>
#include <thread>
#include <mutex>
#include <condition_variable>
static constexpr int K = 8; // number of stalls
unsigned long long stalls[K];
void wait_until_uncomfortable() {
// stoic philosophers are always comfortable
}
[[gnu::noinline]] // prevent compiler optimization
void poop_into(int stall) {
stalls[stall] += 1;
}
void user(int preferred_stall) {
while (true) {
wait_until_uncomfortable();
poop_into(preferred_stall);
}
}
int main() {
std::default_random_engine randomness((std::random_device())());
std::uniform_int_distribution<int> pick_stall(0, K - 1);
for (size_t i = 0; i != 32; ++i) {
std::thread t(user, pick_stall(randomness));
t.detach();
}
sleep(5);
// NB: data races here
for (int i = 0; i != K; ++i) {
printf("stalls[%d] = %llu\n", i, stalls[i]);
}
}