-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathracing.cpp
More file actions
44 lines (35 loc) · 1.07 KB
/
racing.cpp
File metadata and controls
44 lines (35 loc) · 1.07 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
#include <iostream>
#include <thread>
#include <vector>
/*
* This program tries to increment an integer `nInc` times in `nThread` threads.
* If the result comes out at `nInc*nThread`, it stays silent, but it will print
* an error if a race condition is detected.
* If you don't see it racing, try ./run ./racing, which keeps invoking the
* executable until a race condition is detected.
*/
constexpr std::size_t nThread = 10;
constexpr std::size_t nInc = 1000;
constexpr std::size_t nRepeat = 1000;
int main() {
int nError = 0;
for (std::size_t j = 0; j < nRepeat; j++) {
int a = 0;
// Increment the variable a 100 times:
auto increment = [&a](){
for (std::size_t i = 0; i < nInc; ++i) {
a++;
}
};
// Start up all threads
std::vector<std::thread> threads;
for (std::size_t i = 0; i < nThread; ++i) threads.emplace_back(increment);
for (auto & thread : threads) thread.join();
// Check
if (a != nThread * nInc) {
std::cerr << "Race detected! Result: " << a << '\n';
nError++;
}
}
return nError;
}