1. 竞态条件(Race Condition)
问题描述:多个线程同时访问并修改共享资源,导致不可预测的结果。
#include <iostream>
#include <thread>
int counter = 0;
void increment() {
for (int i = 0; i < 1000000; ++i) {
counter++; // 竞态条件:多个线程同时修改 counter
}
}
int main() {
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
std::cout << "Counter: " << counter << std::endl;
return 0;
}
解决方法:使用互斥锁(std::mutex
)保护共享资源。
#include <iostream>
#include <thread>
#include <mutex>
int counter = 0;
std::mutex mtx;
void increment() {
for (int i = 0; i < 1000000; ++i) {
std::unique_lock<std::mutex> lock(mtx); // 使用互斥锁保护共享资源
counter++;
}
}
int main() {
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
std::cout << "Counter: " << counter << std::endl;
return 0;
}
2. 死锁(Deadlock)
问题描述:两个或多个线程互相等待对方释放资源,导致程序无法继续执行。
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx1, mtx2;
void thread1() {
std::unique_lock<std::mutex> lock1(mtx1);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::unique_lock<std::mutex> lock2(mtx2); // 线程1等待mtx2
std::cout << "Thread 1 acquired both locks" << std::endl;
}
void thread2() {
std::unique_lock<std::mutex> lock2(mtx2);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::unique_lock<std::mutex> lock1(mtx1); // 线程2等待mtx1
std::cout << "Thread 2 acquired both locks" << std::endl;
}
int main() {
std::thread t1(thread1);
std::thread t2(thread2);
t1.join();
t2.join();
return 0;
}
解决方法:避免循环等待,按照相同的顺序获取互斥锁。
cpp
Copy code
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx1, mtx2;
void thread1() {
std::unique_lock<std::mutex> lock1(mtx1);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::unique_lock<std::mutex> lock2(mtx2, std::defer_lock); // 延迟加锁
if (lock2.try_lock()) {
std::cout << "Thread 1 acquired both locks" << std::endl;
} else {
std::cout << "Thread 1 failed to acquire lock2" << std::endl;
}
}
void thread2() {
std::unique_lock<std::mutex> lock2(mtx2);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::unique_lock<std::mutex> lock1(mtx1, std::defer_lock); // 延迟加锁
if (lock1.try_lock()) {
std::cout << "Thread 2 acquired both locks" << std::endl;
} else {
std::cout << "Thread 2 failed to acquire lock1" << std::endl;
}
}
int main() {
std::thread t1(thread1);
std::thread t2(thread2);
t1.join();
t2.join();
return 0;
}
这些案例涉及了竞态条件和死锁等多线程编程中常见的问题,通过合适的同步机制和锁的使用,可以避免或减轻这些问题的发生。
解决方法:避免循环等待,按照相同的顺序获取互斥锁。
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx1, mtx2;
void thread1() {
std::unique_lock<std::mutex> lock1(mtx1);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::unique_lock<std::mutex> lock2(mtx2, std::defer_lock); // 延迟加锁
if (lock2.try_lock()) {
std::cout << "Thread 1 acquired both locks" << std::endl;
} else {
std::cout << "Thread 1 failed to acquire lock2" << std::endl;
}
}
void thread2() {
std::unique_lock<std::mutex> lock2(mtx2);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::unique_lock<std::mutex> lock1(mtx1, std::defer_lock); // 延迟加锁
if (lock1.try_lock()) {
std::cout << "Thread 2 acquired both locks" << std::endl;
} else {
std::cout << "Thread 2 failed to acquire lock1" << std::endl;
}
}
int main() {
std::thread t1(thread1);
std::thread t2(thread2);
t1.join();
t2.join();
return 0;
}
这些案例涉及了竞态条件和死锁等多线程编程中常见的问题,通过合适的同步机制和锁的使用,可以避免或减轻这些问题的发生。