C++11開始支持多線程編程,之前多線程編程都需要系統(tǒng)的支持,在不同的系統(tǒng)下創(chuàng)建線程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用起來(lái)都比較復(fù)雜,C++11提供了新頭文件<thread>、<mutex>、<atomic>、<future>等用于支持多線程。
使用C++11開啟一個(gè)線程是比較簡(jiǎn)單的,下面來(lái)看一個(gè)簡(jiǎn)單的例子:
#include <thread>
#include <iostream>
void hello()
{
std::cout << "Hello from thread " << std::endl;
}
int main()
{
std::thread t1(hello);
t1.join();
std::cout<<"Main Thread"<<std::endl;
return 0;
}
運(yùn)行結(jié)果:
說(shuō)明,通過(guò)thread 類直接申明一個(gè)線程t1,參數(shù)是這個(gè)線程執(zhí)行的回調(diào)函數(shù)的地址,通過(guò)jion()方法阻塞主線程,直到t1線程執(zhí)行結(jié)束為止。
C++11支持Lambda表達(dá)式,因此一個(gè)新線程的回調(diào)函數(shù)也可以是有一個(gè)Lambda表達(dá)式的形式,但是注意如果使用Lambda表達(dá)式最好不要使用引用的方式,應(yīng)該使用值傳遞的方式來(lái)訪問(wèn)數(shù)據(jù),在多線程中使用引用容易造成混亂。下面這個(gè)例子稍微復(fù)雜,創(chuàng)建了多個(gè)子線程,并使用了get_id()方法來(lái)獲取當(dāng)前線程的id。
#include <thread>
#include <iostream>
#include <vector>
int main()
{
std::vector<std::thread> threads;
for(int i = 0; i < 5; ++i){
threads.push_back(std::thread([](){
std::cout << "Hello from lamda thread " << std::this_thread::get_id() << std::endl;
}));
}
for(auto& thread : threads){
thread.join();
}
std::cout<<"Main Thread"<<"\t"<<std::this_thread::get_id()<<std::endl;
return 0;
}
運(yùn)行結(jié)果:
上述代碼中,使用vector來(lái)存放每個(gè)線程,線程的回調(diào)函數(shù)通過(guò)Lambda表達(dá)式產(chǎn)生,注意后面join的使用方式。
可以通過(guò)sleep_for來(lái)使線程睡眠一定的時(shí)間:
#include <thread>
#include <iostream>
#include <mutex>
using namespace std;
int main()
{
std::mutex m;
thread t1([&m]()
{
std::this_thread::sleep_for (chrono::seconds(10));
for(int i=0;i<10;i++)
{
m.lock();
cout << "In t1 ThreadID : " << std::this_thread::get_id() << ":" << i << endl;
m.unlock ();
}
} );
thread t2([&m]()
{
std::this_thread::sleep_for (chrono::seconds(1));
for(int i=0;i<10;i++)
{
m.lock ();
cout << "In t2 ThreadID : " << std::this_thread::get_id() << ":" << i << endl;
m.unlock();
}
} );
t1.join();
t2.join();
cout<<"Main Thread"<<endl;
return 0;
}
運(yùn)行結(jié)果:
可以看出,由于線程t1睡眠的時(shí)間較長(zhǎng),t2先執(zhí)行了。
延時(shí)有這幾種類型:nanoseconds、microseconds、milliseconds、seconds、minutes、hours。
在使用多線程的程序中操作共享數(shù)據(jù)的時(shí)候一定要小心,由于線程的亂序執(zhí)行,可能會(huì)得到意想不到的結(jié)果。通過(guò)下面的程序來(lái)看:
#include <thread>
#include <iostream>
#include <vector>
#include <mutex>
struct Counter {
std::mutex mutex;
int value;
Counter() : value(0) {}
void increment(){
// mutex.lock(); 【1】表示沒(méi)有使用鎖
++value;
// mutex.unlock(); 【1】
}
void decrement(){
mutex.lock();
--value;
mutex.unlock();
}
};
int main(){
Counter counter;
std::vector<std::thread> threads;
for(int i = 0; i < 5; ++i){
threads.push_back(std::thread([&](){
for(int i = 0; i < 10000; ++i){
counter.increment();
}
}));
}
for(auto& thread : threads){
thread.join();
}
std::cout << counter.value << std::endl;
return 0;
}
運(yùn)行結(jié)果:
【1】
運(yùn)行結(jié)果:(使用了鎖)
說(shuō)明:由于創(chuàng)建線程是使用lambda表達(dá)式,并使用引用的方式訪問(wèn)counter這個(gè)變量,當(dāng)沒(méi)有使用lock來(lái)保護(hù)的時(shí)候(情況【1】),執(zhí)行的結(jié)果可能不像預(yù)期的5000(程序的意思是每個(gè)線程使counter中的value自加1000次,5個(gè)線程運(yùn)行結(jié)束的時(shí)候應(yīng)該是5000),當(dāng)沒(méi)有使用鎖的時(shí)候自加的操作可能被其他線程打斷,因此結(jié)果可能會(huì)小于5000。
聯(lián)系客服