您的位置:首页 > 编程语言 > C语言/C++

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

2015-12-21 21:37 363 查看
参考连接: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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: