互斥类的最重要成员函数是lock()和unlock()。在进入临界区时,执行lock()加锁操作,如果这时已经被其它线程锁住,则当前线程在此排队等待。退出临界区时,执行unlock()解锁操作。更好的办法是采用”资源分配时初始化”(RAII)方法来加锁、解锁,这避免了在临界区中因为抛出异常或return等操作导致没有解锁就退出的问题。极大地简化了程序员编写mutex相关的异常处理代码。C++11的标准库中提供了std::lock_guard类模板做mutex的RAII。

std::lock_guard类的构造函数禁用拷贝构造,且禁用移动构造。std::lock_guard类除了构造函数和析构函数外没有其它成员函数

在std::lock_guard对象构造时,传入的mutex对象(即它所管理的mutex对象)会被当前线程锁住。在lock_guard对象被析构时,它所管理的mutex对象会自动解锁,不需要程序员手动调用lock和unlock对mutex进行上锁和解锁操作。lock_guard对象并不负责管理mutex对象的生命周期,lock_guard对象只是简化了mutex对象的上锁和解锁操作,方便线程对互斥量上锁,即在某个lock_guard对象的生命周期内,它所管理的锁对象会一直保持上锁状态;而lock_guard的生命周期结束之后,它所管理的锁对象会被解锁。程序员可以非常方便地使用lock_guard,而不用担心异常安全问题。

std::lock_guard在构造时只被锁定一次,并且在销毁时解锁。

关于std::mutex的基础介绍可以参考: http://blog.csdn.net/fengbingchun/article/details/73521630

The difference is that you can lock and unlock a std::unique_lock. std::lock_guard will be locked only once on construction and unlocked on destruction.

std::unique_lock has other features that allow it to e.g.: be constructed without locking the mutex immediately but to build the RAII wrapper. However, std::unique_lock might have a tad more overhead(较多开销).

std::lock_guard also provides a convenient RAII wrapper, but cannot lock multiple mutexes safely. It can be used when you need a wrapper for a limited scope, e.g.: a member function.

One of the differences between std::lock_guard and std::unique_lock is that the programmer is able to unlock std::unique_lock, but she/he is not able to unlock std::lock_guard.

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "lock_guard.hpp"
#include <iostream>
#include <thread>
#include <mutex>
#include <stdexcept>
#include <list>
#include <algorithm>namespace lock_guard_ {///
// reference: http://www.cplusplus.com/reference/mutex/lock_guard/
namespace {
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) {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 test_lock_guard_1()
{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;
}///
// reference: http://www.cplusplus.com/reference/mutex/lock_guard/lock_guard/
namespace {
std::mutex mtx2;           // mutex for critical sectionvoid print_thread_id2(int id) {mtx2.lock();std::lock_guard<std::mutex> lck(mtx2, std::adopt_lock);std::cout << "thread #" << id << '\n';
}
}int test_lock_guard_2()
{std::thread threads[10];// spawn 10 threads:for (int i = 0; i<10; ++i)threads[i] = std::thread(print_thread_id2, i + 1);for (auto& th : threads) th.join();return 0;
}// reference: http://en.cppreference.com/w/cpp/thread/lock_guard
namespace {
int g_i = 0;
std::mutex g_i_mutex;  // protects g_ivoid safe_increment()
{std::lock_guard<std::mutex> lock(g_i_mutex);++g_i;std::cout << std::this_thread::get_id() << ": " << g_i << '\n';// g_i_mutex is automatically released when lock goes out of scope
}
}int test_lock_guard_3()
{std::cout << "main: " << g_i << '\n';std::thread t1(safe_increment);std::thread t2(safe_increment);t1.join();t2.join();std::cout << "main: " << g_i << '\n';return 0;
}//
// reference: http://www.bogotobogo.com/cplusplus/C11/7_C11_Thread_Sharing_Memory.php
namespace {
// a global variable
std::list<int> myList;// a global instance of std::mutex to protect global variable
std::mutex myMutex;void addToList(int max, int interval)
{// the access to this function is mutually exclusivestd::lock_guard<std::mutex> guard(myMutex);for (int i = 0; i < max; i++) {if ((i % interval) == 0) myList.push_back(i);}
}void printList()
{// the access to this function is mutually exclusivestd::lock_guard<std::mutex> guard(myMutex);for (auto itr = myList.begin(), end_itr = myList.end(); itr != end_itr; ++itr) {std::cout << *itr << ",";}
}
}
int test_lock_guard_4()
{int max = 100;std::thread t1(addToList, max, 1);std::thread t2(addToList, max, 10);std::thread t3(printList);t1.join();t2.join();t3.join();return 0;
}} // namespace lock_guard_

GitHub: https://github.com/fengbingchun/Messy_Test

C++11中std::lock_guard的使用相关推荐

  1. Multi-thread--C++11中std::lock_guard的使用

    互斥类的最重要成员函数是lock()和unlock().在进入临界区时,执行lock()加锁操作,如果这时已经被其它线程锁住,则当前线程在此排队等待.退出临界区时,执行unlock()解锁操作.更好的 ...

  2. C++/C++11中std::string用法汇总

    C++/C++11中std::string是个模板类,它是一个标准库.使用string类型必须首先包含<string>头文件.作为标准库的一部分,string定义在命名空间std中. st ...

  3. C++11中std::async的使用

    C++11中的std::async是个模板函数.std::async异步调用函数,在某个时候以Args作为参数(可变长参数)调用Fn,无需等待Fn执行完成就可返回,返回结果是个std::future对 ...

  4. C++11中std::unique_lock的使用

    std::unique_lock为锁管理模板类,是对通用mutex的封装.std::unique_lock对象以独占所有权的方式(unique owership)管理mutex对象的上锁和解锁操作,即 ...

  5. C++11中std::condition_variable的使用

    <condition_variable>是C++标准程序库中的一个头文件,定义了C++11标准中的一些用于并发编程时表示条件变量的类与方法等. 条件变量是并发程序设计中的一种控制结构.多个 ...

  6. Multi-thread--C++11中std::unique_lock的使用

    std::unique_lock为锁管理模板类,是对通用mutex的封装.std::unique_lock对象以独占所有权的方式(unique owership)管理mutex对象的上锁和解锁操作,即 ...

  7. Multi-thread--C++11中std::condition_variable的使用

    <condition_variable>是C++标准程序库中的一个头文件,定义了C++11标准中的一些用于并发编程时表示条件变量的类与方法等. 条件变量是并发程序设计中的一种控制结构.多个 ...

  8. Multi-thread--C++11中std::mutex的使用

    C++11中新增了<mutex>,它是C++标准程序库中的一个头文件,定义了C++11标准中的一些互斥访问的类与方法等.其中std::mutex就是lock.unlock.std::loc ...

  9. c++11中的lock_guard和unique_lock使用浅析

    锁 锁用来在多线程访问同一个资源时防止数据竞险,保证数据的一致性访问. 多线程本来就是为了提高效率和响应速度,但锁的使用又限制了多线程的并行执行,这会降低效率,但为了保证数据正确,不得不使用锁,它们就 ...

最新文章

  1. 模型部署神器:高中生也能读懂的Docker入门教程
  2. windows下pycharm连接vagrant的python环境
  3. shell实例第13讲:从FTP服务器下载文件
  4. 系统 销售订单号_销售单,进货单自动计算,一键保存的进销存管理系统,十分好用...
  5. 让Entity Framework支持MySql数据库
  6. JS判断当前的浏览器类型
  7. jest 单元测试模拟模块设置动态值
  8. matlab计算大米个数,如何求数组元素的个数 matlab
  9. 安卓手机上微信无法打开Https网址的完美解决方案
  10. mysql binlog ignore db_MySQL binlog_ignore_db 参数最全解析
  11. 一个空值_3秒快速、大批量删除或修改Excel中的空值 | 学术小课堂
  12. 【解读】Http协议
  13. clickhouse: WSL下常见问题、常见用法和A股数据实践
  14. 安徽大学(线性代数第一章详细答案)
  15. bolt界面引擎学习笔记一
  16. 苹果ppt_苹果又开发布会了,这几个PPT设计技巧值得学习
  17. Spring中的工厂模式
  18. 用PPT就可以做印章?是的,超简单超逼真,教你一分钟搞定
  19. flash player 11.2 64位 linux,Adobe Flash Player 11.2.202 Beta 1支持 64位操作系统
  20. 杭州治堵有“智慧” 阿里云数加激活城市大脑

热门文章

  1. 智慧工地-基于深度学习yolov3的安全帽检测以及安全帽检测数据集(1)
  2. ROS、realsense开发常用命令汇总
  3. 设备初始化缩水篇(一)
  4. 深度学习--TensorFlow (2)单层感知器2 -- 实现多数据分类
  5. oracle10安装网络需求警告,安装oracle10出现的问题,求解!!!!!(在线等)
  6. 神经网络输出大小(卷积层及池化层)
  7. 剑指offer:面试题26. 树的子结构
  8. coreseek最大检索数只有1000的问题!
  9. leetcode-53 最大子序和
  10. a-awk 计算数值最大,最小,平均值并保留指定位数