Listen to this Post
In a football match:
- Main Referee: Makes decisions on the field.
- VAR (Video Assistant Referee): Reviews decisions in the background.
- Synchronization: The referee waits for the VARโs input before finalizing a decision.
Similarly, in C++:
- std::mutex: Protects shared data (like the refereeโs decision).
- std::condition_variable: Makes one thread wait for another (like the referee waiting for VAR).
- std::thread: Enables concurrent execution (like the referee and VAR working simultaneously).
๐ก What Youโll Learn:
โ How to use `std::mutex` to protect shared resources.
โ Why `std::condition_variable` is essential for thread synchronization.
โ
How to create and manage threads with std::thread.
๐ Key Takeaways:
๐ Use `std::mutex` to prevent data races in multi-threaded programs.
๐ Leverage `std::condition_variable` to coordinate tasks between threads.
๐ Always join threads to ensure proper program execution.
You Should Know:
Hereโs a practical example of using std::mutex, std::condition_variable, and `std::thread` in C++:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex mtx;
std::condition_variable cv;
bool ready = false;
void print_id(int id) {
std::unique_lock<std::mutex> lock(mtx);
while (!ready) {
cv.wait(lock); // Wait for the signal
}
std::cout << "Thread " << id << " is running.\n";
}
void go() {
std::unique_lock<std::mutex> lock(mtx);
ready = true;
cv.notify_all(); // Notify all waiting threads
}
int main() {
std::thread threads[5];
for (int i = 0; i < 5; ++i) {
threads[i] = std::thread(print_id, i);
}
std::cout << "5 threads ready to race...\n";
go(); // Start the threads
for (auto& th : threads) {
th.join();
}
return 0;
}
**Explanation**:
1. **std::mutex**: Protects the shared variable `ready`.
- std::condition_variable: Makes threads wait until `ready` is set to
true.
3. **std::thread**: Creates and manages threads.
### **What Undercode Say**:
Thread synchronization is a critical concept in multi-threaded programming, especially in C++. Using `std::mutex` and `std::condition_variable` ensures that shared resources are accessed safely and threads are coordinated effectively. Always remember to join threads to avoid undefined behavior. For further reading, check out the C++ documentation on threads.
**Related Linux Commands**:
- Use `ps -eLf` to view all threads in a Linux system.
- Use `g++ -std=c++11 -pthread your_program.cpp -o output` to compile C++ programs with threads.
- Use `strace -f ./output` to trace thread execution in Linux.
**Related Windows Commands**:
- Use `tasklist /M` to list all threads in Windows.
- Use `cl /EHsc /MD your_program.cpp` to compile C++ programs with threads in Windows.
- Use `Process Explorer` from Sysinternals to monitor threads in Windows.
By mastering these concepts and tools, you can build efficient and safe multi-threaded applications.
References:
Reported By: Mohamed Khabou – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass โ



