std::mutex std::unique_lock std::lock_guard std::recursive_mutex的理解

来源:互联网 发布:怎样才能加入淘宝商城 编辑:程序博客网 时间:2024/06/05 08:43

std::mutex 称为互斥量;

    C++11中,std::unique_lock, std::lock_guard, std::recursive_mutex可以简单理解为对std::mutex的封装,且对互斥量的unlock是在对象(比如std::unique_lock对象销毁时执行。

    区域锁lock_guard使用起来比较简单,除了构造函数外没有其他member function,在整个区域都有效。
    区域锁unique_lock除了lock_guard的功能外,提供了更多的member_function,相对来说更灵活一些。
    unique_lock的最有用的一组函数为: 


    通过上面的函数,可以通过lock/unlock可以比较灵活的控制锁的范围,减小锁的粒度。
    通过try_lock_for/try_lock_until则可以控制加锁的等待时间,此时这种锁为乐观锁。

下面举两个例子,注意红色标注的部分:

Example 1

12345678910111213141516171819202122232425
// mutex example#include <iostream>       // std::cout#include <thread>         // std::thread#include <mutex>          // std::mutexstd::mutex mtx;           // mutex for critical sectionvoid print_block (int n, char c) {  // critical section (exclusive access to std::cout signaled by locking mtx):  mtx.lock();  for (int i=0; i<n; ++i) { std::cout << c; }  std::cout << '\n';  mtx.unlock();}int main (){  std::thread th1 (print_block,50,'*');  std::thread th2 (print_block,50,'$');  th1.join();  th2.join();  return 0;}
 
Possible output (order of lines may vary, but characters are never mixed):
**************************************************$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Example 2

123456789101112131415161718192021222324
// unique_lock example#include <iostream>       // std::cout#include <thread>         // std::thread#include <mutex>          // std::mutex, std::unique_lockstd::mutex mtx;           // mutex for critical sectionvoid print_block (int n, char c) {  // critical section (exclusive access to std::cout signaled by lifetime of lck):  std::unique_lock<std::mutex> lck (mtx);  for (int i=0; i<n; ++i) { std::cout << c; }  std::cout << '\n';}int main (){  std::thread th1 (print_block,50,'*');  std::thread th2 (print_block,50,'$');  th1.join();  th2.join();  return 0;}
Edit & Run

Possible output (order of lines may vary, but characters are never mixed):
**************************************************$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Example 3

1234567891011121314151617181920212223242526272829303132333435
// lock_guard example#include <iostream>       // std::cout#include <thread>         // std::thread#include <mutex>          // std::mutex, std::lock_guard#include <stdexcept>      // std::logic_errorstd::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) {  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;}
Edit & Run

Possible output:
[exception caught]2 is even[exception caught]4 is even[exception caught]6 is even[exception caught]8 is even[exception caught]
10 is even

0 0