std::mutex::unlock

Defined in header <mutex> - 定义于头文件 <mutex>

public member function - 公开成员函数

mutex:n. 互斥,互斥元,互斥体,互斥量
synchronization [ˌsɪŋkrənaɪˈzeɪʃn]:n. 同步,同时性
primitive [ˈprɪmətɪv]:adj. 原始的,远古的,简单的,粗糙的 n. 原始人
simultaneously [ˌsɪmlˈteɪniəsli]:adv. 同时地
exclusive [ɪkˈskluːsɪv]:adj. 独有的,排外的,专一的 n. 独家新闻,独家经营的项目,排外者
semantics [sɪˈmæntɪks]:n. 语义学,语义论
recursive [rɪˈkɜːsɪv]:adj. 递归的,循环的

1. std::mutex::unlock

void unlock(); - since C++11

Unlock mutex - 解锁 mutex

Unlocks the mutex, releasing ownership over it.
解锁 mutex,释放对它的所有权。

If other threads are currently blocked attempting to lock this same mutex, one of them acquires ownership over it and continues its execution.
如果当前其他线程试图锁定 mutex 被阻塞,则其中一个将获取该线程的所有权并继续执行。

All lock and unlock operations on the mutex follow a single total order, with all visible effects synchronized between the lock operations and previous unlock operations on the same object.
mutex 上的所有锁定和解锁操作都遵循一个总顺序,所有可见效果在同一对象上的锁定操作和先前的解锁操作之间同步。

If the mutex is not currently locked by the calling thread, it causes undefined behavior.
如果 mutex 当前未被调用线程锁定,则将导致未定义的行为。

Unlocks the mutex.
解锁 mutex

The mutex must be locked by the current thread of execution, otherwise, the behavior is undefined.
mutex 必须为当前执行线程所锁定,否则行为未定义。

This operation synchronizes-with (as defined in std::memory_order) any subsequent lock operation that obtains ownership of the same mutex.
此操作同步于 (定义于 std::memory_order) 任何后继的取得同一 mutex 所有权的锁操作。

unlock() is usually not called directly: std::unique_lock and std::lock_guard are used to manage exclusive locking.
通常不直接调用 unlock():用 std::unique_lockstd::lock_guard 管理排他性锁定。

intermingle [ˌɪntəˈmɪŋɡl]:vt. 使混合,使搀和 vi. 混合,掺杂

2. Parameters

none

3. Return value

none

4. Example - 示例

4.1 std::mutex::lock/unlock

//============================================================================
// Name        : std::mutex::lock/unlock
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <mutex>          // std::mutexstd::mutex mtx;           // mutex for critical sectionvoid print_thread_id(int id)
{// critical section (exclusive access to std::cout signaled by locking mtx):mtx.lock();std::cout << "thread #" << id << "-->";std::cout << "thread #" << id << '\n';mtx.unlock();
}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;
}

Possible output (order of lines may vary, but they are never intermingled):
可能的输出 (行的顺序可能会有所不同,但是它们永远不会混合在一起):

thread #1-->thread #1
thread #7-->thread #7
thread #2-->thread #2
thread #9-->thread #9
thread #4-->thread #4
thread #6-->thread #6
thread #5-->thread #5
thread #8-->thread #8
thread #3-->thread #3
thread #10-->thread #10

4.2 std::mutex::lock/unlock

//============================================================================
// Name        : std::mutex::lock
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>int g_num = 0;  // protected by g_num_mutex
std::mutex g_num_mutex;void slow_increment(int id)
{for (int i = 0; i < 5; ++i){g_num_mutex.lock();++g_num;std::cout << id << " => " << g_num << '\n';g_num_mutex.unlock();std::this_thread::sleep_for(std::chrono::seconds(1));}
}int main()
{std::thread t1(slow_increment, 0);std::thread t2(slow_increment, 1);t1.join();t2.join();return 0;
}
1 => 1
0 => 2
1 => 3
0 => 4
1 => 5
0 => 6
1 => 7
0 => 8
1 => 9
0 => 10

5. Data races - 数据竞争

The mutex object is modified as an atomic operation (causing no data races).
互斥对象被修改为原子操作 (不引起数据竞争)。

6. Exception safety - 异常安全性

If the mutex is currently locked by the calling thread, this function never throws exceptions (no-throw guarantee). Otherwise, it causes undefined behavior.
如果 mutex 当前被调用线程锁定,则此函数从不抛出异常 (无抛出保证)。否则,它将导致未定义的行为。

Reference

http://www.cplusplus.com/reference/mutex/mutex/lock/
https://en.cppreference.com/w/cpp/thread/mutex/lock

std::mutex::unlock相关推荐

  1. C++多线程:互斥变量 std::mutex

    文章目录 描述 成员函数 总结 描述 头文件 <mutex> 使用 std::mutex <variable> 简介 mutex是一种多线程变成中的同步原语,它能够让共享数据不 ...

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

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

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

    原文转载于:https://blog.csdn.net/fengbingchun/article/details/73521630 C++11中新增了<mutex>,它是C++标准程序库中 ...

  4. C++11 并发指南三(std::mutex 详解)

    上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::thread 的一些用法,并给出了两个小例子,本文将介绍 std::mutex 的用法. Mutex ...

  5. C++多线程中互斥量std::mutex与模板类std::lock_guard

    一. 互斥量std::mutex C++中通过实例化std::mutex创建互斥量实例,通过成员函数lock()对互斥量上锁,unlock()进行解锁.C++中与std::mutex相关的类(包括锁类 ...

  6. 【多线程】多线程锁住的是什么、std::lock_guard<std::mutex> locker(mutex_)

    通常不直接使用 mutex,lock_guard更加安全, 更加方便. lock_guard简化了 lock/unlock 的写法, lock_guard在构造时自动锁定互斥量, 而在退出作用域时会析 ...

  7. std::mutex

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

  8. std::mutex详解

    Mutex 又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mutex> 头文件中,所以如果你需要使用 std::mutex,就必须包含 <mute ...

  9. C++ std::mutex 用法详解

    Mutex 又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 头文件中,所以如果你需要使用 std::mutex,就必须包含 头文件. 头文件介绍 Mutex 系列类(四 ...

最新文章

  1. 手机app 有没有window.location.href_热议小程序使用场景越来越多,未来有没有可能替代手机APP?...
  2. 加密、解密、摘要、签名、证书一文搞懂
  3. vmware workstation pro 14 虚拟机无法开启、黑屏的解决方案汇总
  4. 16*64点阵屏的c语言程序,16*64点阵程序 - 单片机/MCU论坛 - 电子技术论坛 - 广受欢迎的专业电子论坛!...
  5. 神经网络告诉我,谁是世界上最「美」的人?
  6. i7怎么老是显示无服务器,i7处理器真有这么差?网友:懂电脑的人都不买!
  7. c语言程序设计网络作业,北语网院17春《C语言程序设计》作业_2满分答案
  8. vue-json-editor高度调整
  9. 电力线通信有望在物联网应用中大放异彩
  10. vs2015如何安装vsix扩展工具
  11. JS - Promise使用详解--摘抄笔记
  12. 统一社会信用代码18位数字分别代表什么含义
  13. 整理一个将qq音乐的歌单导入到苹果音乐中的方法
  14. python中str是什么函数_python里的str是什么函数
  15. iphone ios 视频特效,视频合成
  16. perl 产生随机数
  17. 什么是Cherry-Pick IDEA中怎么使用Cherry-Pick
  18. count()--不是单组分组函数
  19. 网页字体转换——实习僧
  20. 轻松搞定Linux环境变量

热门文章

  1. opencv摄像头 vmware虚拟机无法打开摄像头的解决方法
  2. 进入命令提示符窗口+常用DOS命令提示符
  3. 数据堂获得“维科杯·OFweek 2021人工智能行业最具投资价奖”
  4. 科学家量子计算机时间倒流,腾讯内容开放平台
  5. 物联网毕业设计 单片机室内环境温湿度检测设计与实现
  6. 公司副总请各部门经理喝酒,事后有些经理汇报给了老板,没汇报的反而被打压,你怎么看?...
  7. 老司机写的java代码_老司机告诉你高质量的Java代码是怎么练成的?
  8. 图解TCP/IP读书笔记(一)
  9. 经典案例:排除法找BUG 主控ESP32
  10. 视频编解码 — 码控算法