C++如何使用thread類多線程編程

命運是項羽英雄末路自刎烏江時的那一聲仰天長嘯,命運是屈原留在汨羅江畔的那一串串沉痛的叩問,命運是貝多芬在雙耳失聰時指尖下所擊出的那一曲曲悲壯的交響,命運是奧斯特洛夫斯基雙目失明後寫下的那一頁頁輝煌的華章。今天給大家說一下C++的thread編程!

C++11中引入了一個用於多線程操作的thread類

下面進行簡單演示如何使用,以及如果進行多線程同步。

thread簡單示例

複製代碼

#include

#include

#include

using namespace std;

void thread01()

{

for (int i = 0; i < 5; i++)

{

cout << "Thread 01 is working !" << endl;

Sleep(100);

}

}

void thread02()

{

for (int i = 0; i < 5; i++)

{

cout << "Thread 02 is working !" << endl;

Sleep(200);

}

}

int main()

{

thread task01(thread01);

thread task02(thread02);

task01.join();

task02.join();

for (int i = 0; i < 5; i++)

{

cout << "Main thread is working !" << endl;

Sleep(200);

}

system("pause");

}

複製代碼

輸出:

C++如何使用thread類多線程編程

圖1

thread detach不阻塞主線程

兩個子線程並行執行,join函數會阻塞主流程,所以子線程都執行完成之後才繼續執行主線程。可以使用detach將子線程從主流程中分離,獨立運行,不會阻塞主線程:

複製代碼

#include

#include

#include

using namespace std;

void thread01()

{

for (int i = 0; i < 5; i++)

{

cout << "Thread 01 is working !" << endl;

Sleep(100);

}

}

void thread02()

{

for (int i = 0; i < 5; i++)

{

cout << "Thread 02 is working !" << endl;

Sleep(200);

}

}

int main()

{

thread task01(thread01);

thread task02(thread02);

task01.detach();

task02.detach();

for (int i = 0; i < 5; i++)

{

cout << "Main thread is working !" << endl;

Sleep(200);

}

system("pause");

}

複製代碼

輸出:

C++如何使用thread類多線程編程

圖2

使用detach的主線程和兩個子線程並行執行。

大家可以多多評論與轉發,感謝大家的支持!

thread帶參數子線程

在綁定的時候也可以同時給帶參數的線程傳入參數:

複製代碼

#include

#include

#include

using namespace std;

//定義帶參數子線程

void thread01(int num)

{

for (int i = 0; i < num; i++)

{

cout << "Thread 01 is working !" << endl;

Sleep(100);

}

}

void thread02(int num)

{

for (int i = 0; i < num; i++)

{

cout << "Thread 02 is working !" << endl;

Sleep(200);

}

}

int main()

{

thread task01(thread01, 5); //帶參數子線程

thread task02(thread02, 5);

task01.detach();

task02.detach();

for (int i = 0; i < 5; i++)

{

cout << "Main thread is working !" << endl;

Sleep(200);

}

system("pause");

}

複製代碼

輸出:

C++如何使用thread類多線程編程

圖3

多線程同步mutex

多個線程同時對同一變量進行操作的時候,如果不對變量做一些保護處理,有可能導致處理結果異常:

複製代碼

#include

#include

#include

using namespace std;

int totalNum = 100;

void thread01()

{

while (totalNum > 0)

{

cout << totalNum << endl;

totalNum--;

Sleep(100);

}

}

void thread02()

{

while (totalNum > 0)

{

cout << totalNum << endl;

totalNum--;

Sleep(100);

}

}

int main()

{

thread task01(thread01);

thread task02(thread02);

task01.detach();

task02.detach();

system("pause");

}

複製代碼

部分輸出結果:

C++如何使用thread類多線程編程

圖4

有兩個問題,一是有很多變量被重複輸出了,而有的變量沒有被輸出;二是正常情況下每個線程輸出的數據後應該緊跟一個換行符,但這裡大部分卻是另一個線程的輸出。

這是由於第一個線程對變量操作的過程中,第二個線程也對同一個變量進行各操作,導致第一個線程處理完後的輸出有可能是線程二操作的結果。針對這種數據競爭的情況,可以使用線程互斥對象mutex保持數據同步。mutex類的使用需要包含頭文件mutex。

複製代碼

#include

#include

#include

#include

using namespace std;

mutex mu; //線程互斥對象

int totalNum = 100;

void thread01()

{

while (totalNum > 0)

{

mu.lock(); //同步數據鎖

cout << totalNum << endl;

totalNum--;

Sleep(100);

mu.unlock(); //解除鎖定

}

}

void thread02()

{

while (totalNum > 0)

{

mu.lock();

cout << totalNum << endl;

totalNum--;

Sleep(100);

mu.unlock();

}

}

int main()

{

thread task01(thread01);

thread task02(thread02);

task01.detach();

task02.detach();

system("pause");

}

複製代碼

多線程中加入mutex互斥對象之後輸出正常:

C++如何使用thread類多線程編程

圖5

最後你覺得我們的文章對你有幫助,歡迎關注我,可以私信我:久伴,領取學習資料,在評論下方可以關注我的學習群,你可以隨時在上面向我們提問,把你在學習前端過程中所遇到的問題發給我們。我們每天都會按時回覆大家的每一個問題,希望久伴可以伴隨你從入門到專家。


分享到:


相關文章: