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. linux7安装haproxy,Centos7 源码编译安装haproxy
  2. 【LaTeX】E喵的LaTeX新手入门教程(1)准备篇
  3. js移除字符串的中文/空格
  4. Windows11怎么关机重启?Windows11的关机键在哪?
  5. flex 只有仅限于文件系统的 SWF 文件和可信的本地 SWF 文件可以访问本地资源
  6. 微型计算机的逻辑元素,微机原理及应用 宋廷强 微型计算机原理及应用.ppt
  7. java离职证明模板word_离职证明模板Word免费版下载
  8. mysql gtid 还是pxc_PXC中的GTIDs
  9. fences卸载_fences是什么?fences栅栏桌面怎样安装卸载?
  10. 解读 | 数据分析师(含转行)的面试简历如何写?
  11. 广告联盟,拿什么拯救博客?
  12. 聚观早报 | 特斯拉上线Steam平台;苹果CEO库克访问索尼
  13. 计算机二级oracle,关于计算机考试
  14. 使用cv2在图片上绘制点
  15. storm和vgj vgj_DOTA2:Team VGJ宣布解散,再见VGJ!
  16. 阿里云2023届实习生招聘启动啦,快来加入IoT安全吧
  17. cad图形不见了怎么办_CAD全图缩放后图纸不见了或者变成了小点怎么办
  18. 逸佳君:虚拟化云桌面之虚拟机克隆与配置脚本
  19. Scrapy 2.5 中文官方文档
  20. 类的封装继承多态以及virtual interface-SV

热门文章

  1. markdown mermaid
  2. Medium题目总结
  3. 2016年8月31号
  4. C# e.Handled的用法,控制文本框键盘输入
  5. 无线传感网络就业指导
  6. 视频去水印软件哪个好用?
  7. java项目生成manifest_使用Maven生成manifest
  8. 经典面试题-搜狐和讯网Shell面试题
  9. 模糊数学——模糊集合
  10. android通讯录实例(一)