C++11多线程(一):语言层面的并发

参考连接:http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438288.html

目录

1.与 C++11 多线程相关的头文件

2.简单的示例

3.sleep_for,让线程睡一会

4.mutex,互斥加锁


C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用起来都比较复杂,C++11提供了新头文件<thread>、<mutex>、<atomic>、<future>等用于支持多线程,并且有很好的平台移植性。


1.与 C++11 多线程相关的头文件


C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是<atomic> ,<thread>,<mutex>,<condition_variable>和<future>。

<atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_flag,另外还声明了一套 C 风格的原子类型

和与 C 兼容的原子操作的函数。


<thread>:该头文件主要声明了 std::thread 类,另外 std::this_thread 命名空间也在该头文件中。


<mutex>:该头文件主要声明了与互斥量(mutex)相关的类,包括 std::mutex 系列类,std::lock_guard,

 std::unique_lock, 以及其他的类型和函数。


<condition_variable>:该头文件主要声明了与条件变量相关的类,包括 std::condition_variable 和

 std::condition_variable_any。


<future>:该头文件主要声明了 std::promise, std::package_task 两个 Provider 类,以及 std::future 和

 std::shared_future 两个 Future 类,另外还有一些与之相关的类型和函数,std::async() 函数就声明在此头文件中。


2.简单的示例

使用join来阻塞主线程,直到子线程结束;

并使用lambda表达式写子线程调用的函数;

使用mutex来加锁,让子线程依次调用lock与unlock之间的代码;

并将mutex变量,引用的方式传入lambda函数。

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


void hello()
{
	cout << "hellor from thread!" << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	vector<thread> threads;
	mutex mutexVar;
	for (size_t i = 0; i < 5; i++)
	{
		threads.push_back(
			thread([&mutexVar]()//lambda函数 ,并传入mutexVar变量
				{
					mutexVar.lock();//加锁
					cout << "lambda thread id:" << this_thread::get_id() << endl;
					mutexVar.unlock();
				})
			);
	}
	
		
	for (auto& threadVar:threads)
	{
		threadVar.join();//阻塞主线程,等待子线程结束
	}

	cout << "Main thread id:" <<this_thread::get_id()<< endl;
	return 0;

}</span>
输出:

<span style="font-size:18px;">lambda thread id:9044
lambda thread id:1388
lambda thread id:7984
lambda thread id:7144
lambda thread id:7176
Main thread id:6264
请按任意键继续. . .</span>

3.sleep_for,让线程睡一会

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

int _tmain(int argc, _TCHAR* argv[])
{
	
	mutex mutexVar;
	thread thread1([&mutexVar](){
		
		this_thread::sleep_for(chrono::seconds(12));
		for (size_t i = 0; i < 10; i++)
		{
			mutexVar.lock();//加锁
			cout << "thread1 id:" << this_thread::get_id() << ",num:" << i << endl;
			mutexVar.unlock();

		}
		
		
	});

	thread thread2([&mutexVar](){
		
		this_thread::sleep_for(chrono::seconds(1));
		for (size_t i = 0; i < 10; i++)
		{
			mutexVar.lock();//加锁
			cout << "thread2 id:" << this_thread::get_id()<<",num:"<<i << endl;
			mutexVar.unlock();

		}

		
	});
	
	thread1.join();
	thread2.join();


	cout << "Main thread id:" <<this_thread::get_id()<< endl;


	return 0;

}</span>
输出:

<span style="font-size:18px;">thread2 id:8428,num:0
thread2 id:8428,num:1
thread2 id:8428,num:2
thread2 id:8428,num:3
thread2 id:8428,num:4
thread2 id:8428,num:5
thread2 id:8428,num:6
thread2 id:8428,num:7
thread2 id:8428,num:8
thread2 id:8428,num:9
thread1 id:4856,num:0
thread1 id:4856,num:1
thread1 id:4856,num:2
thread1 id:4856,num:3
thread1 id:4856,num:4
thread1 id:4856,num:5
thread1 id:4856,num:6
thread1 id:4856,num:7
thread1 id:4856,num:8
thread1 id:4856,num:9
Main thread id:8744
请按任意键继续. . .</span>
由于thread1的sleep时间较长,thread2先输出

4.mutex,互斥加锁

<span style="font-size:18px;">int _tmain(int argc, _TCHAR* argv[])
{
	
	mutex mutexVar;
	vector<thread> threads;
	int nTextNum = 0;
	for (size_t i = 0; i < 5; i++)
	{
		threads.push_back(
			thread([&mutexVar, &nTextNum](){  //lambda

			
			for (size_t i = 0; i < 10000; i++)
			{
				mutexVar.lock();//加锁
				auto fun = [&nTextNum]() //lambda
				{
					
					nTextNum++;
				};
				fun();//调用lambda函数
				mutexVar.unlock();

			}
		})
			);
		
	}

	for (auto& threadVar:threads)
	{
		threadVar.join();
	}

	cout << "Main thread id:" << this_thread::get_id() << endl;
	cout << "nTextNum:" << nTextNum << endl;
	return 0;

}</span>
输出:

<span style="font-size:18px;">Main thread id:1760
nTextNum:50000
请按任意键继续. . .</span>
如果屏蔽lock(),输出值nTextNum<50000,共享数据操作应该使用mutex。






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值