-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex_demo.cpp
More file actions
68 lines (59 loc) · 1.99 KB
/
Copy pathmutex_demo.cpp
File metadata and controls
68 lines (59 loc) · 1.99 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @file mutex_demo.cpp
* @brief Demonstrates how a Mutex prevents Race Conditions.
*
* THE PROBLEM:
* Two threads both increment a shared counter 100,000 times each.
* Without synchronization, the final value is unpredictable because
* both threads can read/write the counter simultaneously (race condition).
*
* THE SOLUTION:
* A std::mutex ensures only one thread can access the counter at a time.
* std::lock_guard is an RAII wrapper — it locks in the constructor and
* automatically unlocks in the destructor (even if an exception occurs).
*
* EXPECTED OUTPUT:
* Counter = 200000 (always correct with the mutex)
*
* TRY THIS:
* Comment out the lock_guard line and run again. You'll see the counter
* is less than 200000, proving the race condition exists.
*
* BUILD & RUN:
* cmake -B build -S . && cmake --build build && ./build/test_sync_mutex
*/
#include <iostream>
#include <thread>
#include <mutex>
// A mutex (MUTual EXclusion) — only one thread can lock it at a time.
std::mutex mutexCounter;
// Shared resource — both threads will increment this.
int counter = 0;
/**
* @brief Increments the shared counter 100,000 times.
*
* Each increment is protected by a lock_guard, which:
* 1. Locks the mutex when created (entering critical section)
* 2. Unlocks the mutex when destroyed (leaving critical section)
*/
void increment()
{
for (int i = 0; i < 100000; i++)
{
// lock_guard: Acquires the lock here. Released at end of scope (}).
std::lock_guard<std::mutex> lock(mutexCounter);
// CRITICAL SECTION — only one thread executes this at a time.
counter++;
}
}
int main()
{
// Create two threads, both running the increment function.
std::thread t1(increment);
std::thread t2(increment);
// Wait for both threads to finish before reading the result.
t1.join();
t2.join();
// With the mutex, this is guaranteed to be 200000.
std::cout << "Counter = " << counter << std::endl;
}