cpp
#include <thread>
#include <mutex>
std::mutex mtx;
int counter = 0;
void increment(int n) {
for (int i = 0; i < n; ++i) {
std::lock_guard<std::mutex> lock(mtx); // RAII lock
++counter; // vùng critical section
} // lock tự mở khi ra scope
}
std::thread t1(increment, 1000);
std::thread t2(increment, 1000);
t1.join(); // chờ t1 hoàn thành
t2.join(); // chờ t2 hoàn thành
// counter == 2000 — thread-safeCác vấn đề cơ bản:
| Vấn đề | Mô tả | Giải pháp |
|---|---|---|
| Race condition | 2 thread cùng read-write shared data | mutex |
| Deadlock | 2 thread chờ nhau mãi | std::lock() + lock nhiều mutex cùng lúc |
| Thread leak | join()/detach() không được gọi | destructor của std::thread gọi terminate() |
std::lock_guard vs std::unique_lock: lock_guard đơn giản, không unlock sớm được; unique_lock linh hoạt hơn (có thể unlock(), dùng với condition variable).
cpp
#include <thread>
#include <mutex>
std::mutex mtx;
int counter = 0;
void increment(int n) {
for (int i = 0; i < n; ++i) {
std::lock_guard<std::mutex> lock(mtx); // RAII lock
++counter; // critical section
} // lock released automatically on scope exit
}
std::thread t1(increment, 1000);
std::thread t2(increment, 1000);
t1.join(); // wait for t1 to finish
t2.join(); // wait for t2 to finish
// counter == 2000 — thread-safeCore issues:
| Issue | Description | Solution |
|---|---|---|
| Race condition | 2 threads concurrently read-write shared data | mutex |
| Deadlock | 2 threads wait on each other forever | std::lock() to acquire multiple mutexes atomically |
| Thread leak | join()/detach() not called | std::thread destructor calls terminate() |
std::lock_guard vs std::unique_lock: lock_guard is simple, cannot unlock early; unique_lock is flexible (supports unlock(), works with condition variables).