-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrace_condition.cpp
More file actions
44 lines (39 loc) · 1.19 KB
/
Copy pathrace_condition.cpp
File metadata and controls
44 lines (39 loc) · 1.19 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
// Demonstrates §4.1.2 of docs/multithreading.md: the lost-update race.
// Compiled normally this is undefined behavior; the value will typically be
// less than the expected total. The fix is shown in mutex.cpp.
#include <iostream>
#include <thread>
#include <vector>
class Wallet {
public:
int balance = 0;
// Not atomic: read -> add -> write is three steps, not one.
void deposit(int amount) { balance = balance + amount; }
};
constexpr int kThreads = 8;
constexpr int kDepositsPerThread = 100'000;
constexpr int kExpected = kThreads * kDepositsPerThread;
int run_one_trial() {
Wallet w;
{
std::vector<std::jthread> threads;
for (int i = 0; i < kThreads; ++i)
threads.emplace_back([&] {
for (int j = 0; j < kDepositsPerThread; ++j)
w.deposit(1);
});
} // jthreads join here
return w.balance;
}
int main() {
int wrong = 0;
for (int trial = 0; trial < 10; ++trial) {
int balance = run_one_trial();
if (balance != kExpected) {
std::cout << "trial " << trial << ": balance = " << balance
<< " (lost " << kExpected - balance << " updates)\n";
++wrong;
}
}
std::cout << wrong << " / 10 trials had lost updates\n";
}