C++11多线程(三):std:mutex

参考链接:http://www.cnblogs.com/haippy/p/3237213.html

目录

1.Mutex头文件介绍

2.std::mutex 介绍

3.std::mutex示例

4.std::recursive_mutex 介绍

5.std::time_mutex 介绍

6.std::recursive_timed_mutex 介绍

7.std::lock_guard 介绍

8.std::unique_lock 介绍



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

1.<mutex> 头文件介绍

Mutex 系列类(四种)

std::mutex,最基本的 Mutex 类。
std::recursive_mutex,递归 Mutex 类。
std::time_mutex,定时 Mutex 类。
std::recursive_timed_mutex,定时递归 Mutex 类。


Lock 类(两种)
std::lock_guard,与 Mutex RAII 相关,方便线程对互斥量上锁。
std::unique_lock,与 Mutex RAII 相关,方便线程对互斥量上锁,但提供了更好的上锁和解锁控制。


其他类型
std::once_flag
std::adopt_lock_t
std::defer_lock_t
std::try_to_lock_t


函数
std::try_lock,尝试同时对多个互斥量上锁。
std::lock,可以同时对多个互斥量上锁。
std::call_once,如果多个线程需要同时调用某个函数,call_once 可以保证多个线程对该函数只调用一次。



2.std::mutex 介绍


下面以 std::mutex 为例介绍 C++11 中的互斥量用法。
std::mutex 是C++11 中最基本的互斥量,std::mutex 对象提供了独占所有权的特性——即不支持递归地对 std::mutex 对象上锁,而 std::recursive_lock 则可以递归地对互斥量对象上锁。


std::mutex 的成员函数
1.构造函数,std::mutex不允许拷贝构造,也不允许 move 拷贝,最初产生的 mutex 对象是处于 unlocked 状态的。


2.lock(),调用线程将锁住该互斥量。线程调用该函数会发生下面 3 种情况:

(1). 如果该互斥量当前没有被锁住,则调用线程将该互斥量锁住,直到调用 unlock之前,该线程一直拥有该锁。

(2). 如果当前互斥量被其他线程锁住,则当前的调用线程被阻塞住。

(3). 如果当前互斥量被当前调用线程锁住,则会产生死锁(deadlock)。


3.unlock(), 解锁,释放对互斥量的所有权。


4.try_lock(),尝试锁住互斥量,如果互斥量被其他线程占有,则当前线程也不会被阻塞。线程调用该函数也会出现下面 3 种情况:

(1). 如果当前互斥量没有被其他线程占有,则该线程锁住互斥量,直到该线程调用 unlock 释放互斥量。

(2). 如果当前互斥量被其他线程锁住,则当前调用线程返回 false,而并不会被阻塞掉。

(3). 如果当前互斥量被当前调用线程锁住,则会产生死锁(deadlock)


3.std::mutex示例

try_lock 及lock使用区别

try_lock

<span style="font-size:18px;">#include <iostream>
using namespace std; 
#include <thread>
#include <vector>
#include <mutex>

mutex mtx;
int counter = 0;
void attempt_10k_increases() 
{
	for (int i = 0; i<10; ++i) 
	{
		if (mtx.try_lock()) 
		{   // only increase if currently not locked:
			//cout << this_thread::get_id() << endl;
			++counter;
			mtx.unlock();
		}
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	thread threads[10];
	for (size_t i = 0; i < 10; i++)
	{
		threads[i] = thread(attempt_10k_increases);
	}
	for(auto& th : threads)
	{
		th.join();
	}
	cout << counter << endl;

	return 0;
}</span>
输出:counter 10


换用lock:

void attempt_10k_increases() 
{
	for (int i = 0; i<10; ++i) 
	{
		mtx.lock();
		//cout << this_thread::get_id() << endl;
		++counter;
		mtx.unlock();
	}
}
输出:counter 100


4.std::recursive_mutex 介绍


std::recursive_mutex 与 std::mutex 一样,也是一种可以被上锁的对象,但是和 std::mutex 不同的是,std::recursive_mutex 允许同一个线程对互斥量多次上锁(即递归上锁),来获得对互斥量对象的多层所有权,std::recursive_mutex 释放互斥量时需要调用与该锁层次深度相同次数的 unlock(),可理解为 lock() 次数和 unlock() 次数相同,除此之外,std::recursive_mutex 的特性和 std::mutex 大致相同。


5.std::time_mutex 介绍
std::time_mutex 比 std::mutex 多了两个成员函数,try_lock_for(),try_lock_until()。


try_lock_for 函数接受一个时间范围,表示在这一段时间范围之内线程如果没有获得锁则被阻塞住(与 std::mutex 的 try_lock() 不同,try_lock 如果被调用时没有获得锁则直接返回 false),如果在此期间其他线程释放了锁,则该线程可以获得对互斥量的锁,如果超时(即在指定时间内还是没有获得锁),则返回 false。


try_lock_until 函数则接受一个时间点作为参数,在指定时间点未到来之前线程如果没有获得锁则被阻塞住,如果在此期间其他线程释放了锁,则该线程可以获得对互斥量的锁,如果超时(即在指定时间内还是没有获得锁),则返回 false。

try_lock_for示例:

timed_mutex mtx;
int counter = 0;
void test() 
{

	while (!mtx.try_lock_for(chrono::milliseconds(200)))
	{
		cout << this_thread::get_id() << endl;
		++counter;
	}
	this_thread::sleep_for(chrono::milliseconds(1000));
	mtx.unlock();
		
}
int _tmain(int argc, _TCHAR* argv[])
{
	thread threads[10];
	for (size_t i = 0; i < 10; i++)
	{
		threads[i] = thread(test);
	}
	for (auto &th : threads)
	{
		th.join();
	}
	cout <<"counter:"<< counter << endl;

}
try_lock_until示例:

mtx.try_lock_until(chrono::steady_clock::now()+chrono::seconds(3))

6.std::recursive_timed_mutex 介绍


和 std:recursive_mutex 与 std::mutex 的关系一样,std::recursive_timed_mutex 的特性也可以从 std::timed_mutex 推导出来

7.std::lock_guard 介绍


与 Mutex RAII(资源获取就是初始化) 相关,方便线程对互斥量上锁。lock_guard构造函数上锁,生命期结束时,析构函数解锁。

#include "stdafx.h"
#include <iostream>
using namespace std; 
#include <thread>
#include <vector>
#include <mutex>

mutex mtx;
void test() 
{
	lock_guard<mutex> lockg(mtx);
	cout << this_thread::get_id() << endl;
	this_thread::sleep_for(chrono::milliseconds(1000));		
}
int _tmain(int argc, _TCHAR* argv[])
{
	thread threads[10];
	for (size_t i = 0; i < 10; i++)
	{
		threads[i] = thread(test);
	}
	for (auto &th : threads)
	{
		th.join();
	}
}

8.std::unique_lock 介绍

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


mutex mtx;
void test() 
{
	unique_lock<mutex> lockg(mtx);
	cout << this_thread::get_id() << endl;
	this_thread::sleep_for(chrono::milliseconds(1000));		
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值