std::mutex::unlock

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::mutex

std::mutex mtx;           // mutex for critical section

void 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

发布了454 篇原创文章 · 获赞 1736 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104462755