-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathz-bank-2.cpp
39 lines (31 loc) · 1.08 KB
/
z-bank-2.cpp
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
#include <iostream>
#include <thread>
#include <vector>
volatile int sharedCounter = 0; // Marked volatile to discourage compiler optimizations
void incrementCounter() {
for (int i = 0; i < 100000; ++i) {
// Each thread increments the sharedCounter by its threadID + 1
// This makes the operation less predictable and harder to optimize out
sharedCounter += 1;
sharedCounter -= 1;
}
}
int main() {
const int numThreads = 2;
std::vector<std::thread> threads;
for (int i = 0; i < numThreads; ++i) {
threads.push_back(std::thread(incrementCounter));
}
for (auto& thread : threads) {
thread.join();
}
// The expected result would be the sum of arithmetic series multiplied by the loop count
// However, due to data races, the actual result may differ
int expectedCounter = 0;
for (int i = 1; i <= numThreads; ++i) {
expectedCounter += 100000;
}
std::cout << "Expected counter: " << expectedCounter << std::endl;
std::cout << "Actual counter: " << sharedCounter << std::endl;
return 0;
}