C++多線程

目錄

一、簡單多線程實例(使用join())ios

二、簡單多線程實例(使用detach())編程

三、帶參子線程網絡

四、多線程競爭數據,使用mutex阻止多線程之間數據的競爭多線程

五、網絡編程&多線程的講解ide

六、Qt&Socket網絡編程函數

一、簡單多線程實例(使用join())

使用thread建立子線程,並立馬使用jion()阻塞住,直到子線程執行完畢;(兩個子線程並行執行,join函數會阻塞主流程,因此子線程都執行完成以後才繼續執行主線程)代碼以下:spa

 1 #include <iostream>
 2 #include <thread>
 3 #include <Windows.h>
 4 
 5 using namespace std;
 6 
 7 void thread01()
 8 {
 9     cout << "Thread 01 is working !" << endl;
10     Sleep(100);
11     cout << "Thread 01 is sleeping over !" << endl;
12 }
13 void thread02()
14 {
15     cout << "Thread 02 is working !" << endl;
16     Sleep(100);
17     cout << "Thread 02 is sleeping over !" << endl;
18 }
19 
20 int main()
21 {
22     thread task01(thread01);  //建立子線程並執行該子線程
23     thread task02(thread02);
24     task01.join();  //等待線程task01執行完畢以後,再去執行下一句
25     task02.join();  //等待線程task02執行完畢以後,再去執行下一句
26     for (int i = 0; i < 2; i++)
27     {
28         cout << "Main thread is working !" << endl;
29         Sleep(200);
30     }
31 
32     system("pause");
33 }
簡單子線程實例

執行結果:.net

 執行第二遍:線程

 二、簡單多線程實例---使用detach()

detach將子線程從主流程中分離,獨立運行,不會阻塞主線程:code

 1 #include <iostream>
 2 #include <thread>
 3 #include <Windows.h>
 4 
 5 using namespace std;
 6 
 7 void thread01()
 8 {
 9     cout << "Thread 01 is working !" << endl;
10     Sleep(100);
11     cout << "Thread 01 is sleeping over !" << endl;
12 }
13 void thread02()
14 {
15     cout << "Thread 02 is working !" << endl;
16     Sleep(100);
17     cout << "Thread 02 is sleeping over !" << endl;
18 }
19 
20 int main()
21 {
22     thread task01(thread01);  //建立子線程並執行該子線程
23     thread task02(thread02);
24     task01.detach();  //等待線程task01執行完畢以後,再去執行下一句
25     task02.detach();  //等待線程task02執行完畢以後,再去執行下一句
26     for (int i = 0; i < 2; i++)
27     {
28         cout << "Main thread is working !" << endl;
29         Sleep(200);
30     }
31 
32     system("pause");
33 }
簡單多線程實例---使用detach()

三、帶參子線程

 1 #include <iostream>
 2 #include <thread>
 3 #include <Windows.h>
 4 
 5 using namespace std;
 6 
 7 void thread01(int num)
 8 {
 9     cout << "Thread 01 is working !" << endl;
10     cout << "Thread01's sleeping time is " << num << endl;
11     Sleep(100);
12     cout << "Thread 01 is sleeping over !" << endl;
13 }
14 void thread02(int num)
15 {
16     cout << "Thread 02 is working !" << endl;
17     cout << "Thread02's sleeping time is " << num << endl;
18     Sleep(300);
19     cout << "Thread 02 is sleeping over !" << endl;
20 }
21 
22 int main()
23 {
24     thread task01(thread01,100);  //建立子線程並執行該子線程
25     thread task02(thread02,300);
26     task01.join();  //等待線程task01執行完畢以後,再去執行下一句
27     task02.join();  //等待線程task02執行完畢以後,再去執行下一句
28     for (int i = 0; i < 2; i++)
29     {
30         cout << "Main thread is working !" << endl;
31         Sleep(200);
32     }
33 
34     system("pause");
35 }
帶參子線程

 四、多線程競爭數據,使用mutex阻止多線程之間數據的競爭(互斥量)

由於在線程1和線程2只均改變了全局變量totalNum
爲了在使多線程之間相互影響,須要聲明一個互斥對象保持數據同步

互斥量技術從字面也能夠理解,就是臨界區有線程訪問,其它線程就得排隊等待,它們的訪問是互斥的,實現方式就是給臨界區加鎖與釋放鎖。

 1 #include <iostream>
 2 #include <thread>
 3 #include <Windows.h>
 4 #include <mutex>
 5 
 6 using namespace std;
 7 int totalNum = 100;
 8 
 9 /*
10 由於在線程1和線程2只均改變了全局變量totalNum
11 爲了在使多線程之間相互影響,須要聲明一個互斥對象保持數據同步
12 */
13 std::mutex mu;  //聲明線程互斥對象
14 
15 void thread01(int num)
16 {
17     mu.lock();  //同步數據鎖
18     cout << totalNum << endl;
19     totalNum--;
20     Sleep(100);
21     mu.unlock();  //解除鎖定
22 }
23 void thread02(int num)
24 {
25     mu.lock();  //同步數據鎖
26     cout << totalNum << endl;
27     totalNum--;
28     Sleep(300);
29     mu.unlock();  //解除鎖定
30 }
31 
32 int main()
33 {
34     thread task01(thread01,100);  //建立子線程並執行該子線程
35     thread task02(thread02,300);
36     task01.join();  //等待線程task01執行完畢以後,再去執行下一句
37     task02.join();  //等待線程task02執行完畢以後,再去執行下一句
38     for (int i = 0; i < 2; i++)
39     {
40         cout << "Main thread is working !" << endl;
41         Sleep(200);
42     }
43 
44     system("pause");
45 }
metux阻止多線程數據相互影響

上面代碼中,子函數Tread01()和Tread02()中的語句:totalNum--;即臨界區。

不一樣線程之間相互有數據干擾的地方就是臨界區。(或引發問題的地方)

五、網絡編程&多線程的講解

 

參考博客:

https://blog.csdn.net/qq_42564846/article/details/82736100  參考了《TCP/IP網絡編程 ---尹聖雨》

六、Qt&Socket網絡編程

 

參考博客:

https://blog.csdn.net/u014252478/article/details/80377103

相關文章
相關標籤/搜索