lock_gurad 是c++11 提供的一个新类,是一个区域锁, 只在作用域内起作用, 出了作用域就自动解锁。
看起来应该是比较简单的,但是异常配合起来使用就需要多加注意。
下面的代码是cplusplus.com 提供的sample。
这个sample的本意是启动10个线程,每个线程对1个数字判断奇偶,
如果是偶数就打印 “x is even”(x 是要判断的数字),如果是奇数就打印[exception caught]。
但由于sample 中, std::lock_guard<std::mutex> lck (mtx) 是在try 的作用域中打印的,
一旦print_even 抛出异常,由catch 捕获到,luck就将解锁。
有时候,不同线程的 std::cout << "[exception caught]\n"; 和 std::cout << x << " is even\n";
会同时运行, 这样导致 打印出现混乱。
例如:
[6e xicse petvieonn
caught]
[exception caught]
2 is even
8 is even
4 is even
[exception caught]
[exception caught]
[exception caught]
10 is even
将 std::lock_guard<std::mutex> lck (mtx);
从try 中移到 try 范围外就没问题了。
在函数 print_thread_id 中在try 中调用
实际coding , 应该需要判断一下 异常处理和try 中的代码是否会有冲突,
如果没有则sample code 是没问题的, 如果有 还要另想其他方法。
// lock_guard example
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex, std::lock_guard
#include <stdexcept> // std::logic_error
std::mutex mtx;
void print_even (int x) {
if (x%2==0) std::cout << x << " is even\n";
else throw (std::logic_error("not even"));
}
void print_thread_id (int id) {
std::lock_guard<std::mutex> lck (mtx);
try {
// using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:
//std::lock_guard<std::mutex> lck (mtx);
print_even(id);
}
catch (std::logic_error&) {
std::cout << "[exception caught]\n";
}
}
int main ()
{
std::thread threads[10];
// spawn 10 threads:
for (int i=0; i<10; ++i)
threads[i] = std::thread(print_thread_id,i+1);
for (auto& th : threads) th.join();
return 0;
}
~
~