C++11 提供了如下的4种互斥量,使用时需要包含头文件#include<mutex>
- std::mutex: 独占的互斥量,不能递归使用
- std::timed_mutex 带超时的独占互斥量,不能递归使用
- std::recursive_mutex: 递归互斥量,不带超时功能
- std::recursive_timed_mutex: 带超时的递归互斥量
std::mutex: 独占的互斥量
- 用lock,unlock 进行独占。此时要保证锁被正确释放,如lock到unlock之间程序异常而退出的情况,否则会造成死锁等问题。
- 用try_lock可以尝试获得锁,返回bool值
- 也可以使用lock_guard简化lock/unlock写法,这种写法也更安全,它会保证出了作用域之后锁被释放
#include<iostream>
#include<thread>
#include<chrono>
#include<mutex>
std::mutex g_lock;
void f() {
g_lock.lock();
std::cout << "entered thread" << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "leaving thread" << std::this_thread::get_id() << std::endl;
g_lock.unlock();
}
void f2() {
std::lock_guard<std::mutex> locker(g_lock);
std::cout << "entered thread" << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "leaving thread" << std::this_thread::get_id() << std::endl;
}
int main() {
std::thread threads[6];
for(int i = 0; i < 3; i++)threads[i] = std::thread(f);
for(int i = 3; i < 6; i++)threads[i] = std::thread(f2);
for(int i = 0; i < 6; i++)threads[i].join();
}
递归互斥量std::recursive_mutex
- 当同一个线程多次获取锁的时候,使用mutex会造成死锁。std::recursive_mutex允许同一线程多次获取锁
- 不建议使用递归互斥量,因为它的效率比较低,而且应用场景通常可以通过优化来避免。当获取到达一定的次数后会抛出std::system错误。
#include<iostream>
#include<thread>
#include<chrono>
#include<mutex>
template<typename T>
struct Test {
//std::mutex mutex; //死锁
T mutex;
void A(int x) {
std::lock_guard<T> lock(mutex);
std::cout << 1 << std::endl;
}
void B(int y) {
std::lock_guard<T> lock(mutex);
std::cout << 2 << std::endl;
}
void C(int x, int y) {
std::lock_guard<T> lock(mutex);
A(x);
B(y);
}
};
int main() {
Test<std::mutex> test;
Test<std::recursive_mutex> test2;
//test.C(23,32); //这个会发生死锁
test2.C(23,32);
}
带超时的互斥量 std::time_mutex和std::recursive_timed_mutex
- std::time_mutex是带超时的独占锁
- std::recursive_timed_mutex 是带超时的递归锁
#include<iostream>
#include<thread>
#include<chrono>
#include<mutex>
std::timed_mutex mutex;
void work(int _timeout, int _sleepDuration) {
std::chrono::milliseconds timeout(_timeout);
std::chrono::milliseconds sleepDuration(_sleepDuration);
while(1) {
if(mutex.try_lock_for(timeout)) {
std::cout << std::this_thread::get_id() << ":get lock" << std::endl;
std::this_thread::sleep_for(sleepDuration);
mutex.unlock();
} else {
std::cout << std::this_thread::get_id() << ":fail to get lock" << std::endl;
std::this_thread::sleep_for(sleepDuration);
}
}
}
int main() {
std::thread t1(work, 500, 300);
std::thread t2(work, 500, 300);
t1.join();
t2.join();
}