-
Notifications
You must be signed in to change notification settings - Fork 0
/
race_condition_mutex.cpp
41 lines (31 loc) · 872 Bytes
/
race_condition_mutex.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
40
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
using namespace std;
int counter = 0;
mutex counter_mutex;
void increment(int num_iterations) {
for (int i = 0; i < num_iterations; ++i) {
// Acquire the mutex
lock_guard<mutex> lock(counter_mutex);
// Critical section
++counter;
} // Release the mutex automatically when lock goes out of scope
}
int main() {
const int num_threads = 10;
const int num_iterations = 1000;
vector<thread> threads;
// Start multiple threads
for (int i = 0; i < num_threads; ++i) {
threads.push_back(thread(increment, num_iterations));
}
// Wait for all threads to finish
for (auto& t : threads) {
t.join();
}
// Output the value of counter
cout << "Final counter value: " << counter << endl;
return 0;
}