如何使用lock_guard

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;

}

~                                                                                                                                                                                                         

~                

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值