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

C++11 多线程

2016-05-26 16:32 471 查看
C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用起来都比较复杂,C++11提供了新头文件
<thread>、<mutex>、<atomic>、<future>
等用于支持多线程。

使用C++11开启一个线程是比较简单的,下面来看一个简单的例子:

#include <thread>
#include <iostream>
using namespace std;

void hello()
{
cout << "Hello from thread " <<endl;
}
int main()
{
thread t1(hello);//参数是这个线程执行的回调函数的地址
t1.join();//通过jion()方法阻塞主线程,直到t1线程执行结束为止
cout<<"Main Thread"<<endl;
return 0;
}


结果:

Hello from thread
Main Thread

Process returned 0 (0x0)   execution time : 5.330 s
Press any key to continue.


以上程序通过thread类直接声明一个线程t1,参数是这个线程执行的回调函数的地址,通过jion()方法阻塞主线程,直到t1线程执行结束为止。

C++11支持Lambda表达式,因此一个新线程的回调函数也可以是有一个Lambda表达式的形式,但是注意如果使用Lambda表达式最好不要使用引用的方式,应该使用值传递的方式来访问数据,在多线程中使用引用容易造成混乱。下面这个例子稍微复杂,创建了多个子线程,并使用了get_id()方法来获取当前线程的id。

#include <thread>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<thread> threads;
for(int i = 0; i < 5; ++i)
{
threads.push_back(
thread([](){cout<<"Hello from lamda thread " <<this_thread::get_id()<<endl;})
);
}
for(auto& thread:threads)
{
thread.join();
}
cout<<"Main Thread"<<"\t"<<this_thread::get_id()<<endl;

return 0;
}


上述代码中,使用vector来存放每个线程,线程的回调函数通过Lambda表达式产生,注意后面join的使用方式。

结果:

Hello from lamda thread 2
Hello from lamda thread 3
Hello from lamda thread 4
Hello from lamda thread 5
Hello from lamda thread 6
Main Thread     1

Process returned 0 (0x0)   execution time : 0.493 s
Press any key to continue.


使用多线程的程序中操作共享数据的时候一定要小心,由于线程的乱序执行,可能会得到意想不到的结果。因此要加锁。

#include <thread>
#include <iostream>
#include <vector>
#include <mutex>
using namespace std;

struct Counter {
mutex mutex;
int value;
Counter():value(0){}
void increment()
{
mutex.lock();//(1)使用锁
++value;
mutex.unlock();//(2)解锁
}
void decrement()
{
mutex.lock();
--value;
mutex.unlock();
}
};

int main()
{
Counter counter;
vector<thread> threads;
for(int i = 0; i < 5; ++i){
threads.push_back(thread([&](){
for(int i = 0; i < 100; ++i){
counter.increment();
}
}));
}
for(auto& thread : threads){
thread.join();
}
cout << counter.value << endl;
return 0;
}


由于创建线程是使用lambda表达式,并使用引用的方式访问counter这个变量,当没有使用lock来保护的时候,执行的结果可能不像预期的500(程序的意思是每个线程使counter中的value自加100次,5个线程运行结束的时候应该是500),当没有使用锁的时候自加的操作可能被其他线程打断,因此结果可能会小于500。

转自http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438288.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: