C++11開始支援多線程程式設計,之前多線程程式設計都需要系統的支援,在不同的系統下建立線程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用起來都比較複雜,C++11提供了新頭檔案<thread>、<mutex>、<atomic>、<future>等用于支援多線程。
使用C++11開啟一個線程是比較簡單的,下面來看一個簡單的例子:
#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;
}
運作結果:

說明,通過thread 類直接申明一個線程t1,參數是這個線程執行的回調函數的位址,通過jion()方法阻塞主線程,直到t1線程執行結束為止。
C++11支援Lambda表達式,是以一個新線程的回調函數也可以是有一個Lambda表達式的形式,但是注意如果使用Lambda表達式最好不要使用引用的方式,應該使用值傳遞的方式來通路資料,在多線程中使用引用容易造成混亂。下面這個例子稍微複雜,建立了多個子線程,并使用了get_id()方法來擷取目前線程的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;
}
運作結果:
上述代碼中,使用vector來存放每個線程,線程的回調函數通過Lambda表達式産生,注意後面join的使用方式。
可以通過sleep_for來使線程睡眠一定的時間:
#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;
}
運作結果:
可以看出,由于線程t1睡眠的時間較長,t2先執行了。
nanoseconds、microseconds、milliseconds、seconds、minutes、hours。
在使用多線程的程式中操作共享資料的時候一定要小心,由于線程的亂序執行,可能會得到意想不到的結果。通過下面的程式來看:
#include <thread>
#include <iostream>
#include <vector>
#include <mutex>
struct Counter {
std::mutex mutex;
int value;
Counter() : value(0) {}
void increment(){
// mutex.lock(); 【1】表示沒有使用鎖
++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;
}
運作結果:
【1】
運作結果:(使用了鎖)