1. 程式人生 > >26.多線程join detach

26.多線程join detach

cout sleep body post OS detach RR name class

 1 #include <iostream>
 2 #include <thread>
 3 #include <array>
 4 #include <Windows.h>
 5 using namespace std;
 6 
 7 void show()
 8 {
 9     MessageBoxA(0, "1", "1", 0);
10 }
11 
12 void main()
13 {
14     //獲取CPU核心的個數
15     auto n = thread::hardware_concurrency();
16     cout << "
CPU核心個數:" << n << endl; 17 array<thread, 3> threads{thread(show), thread(show), thread(show)}; 18 19 for (int i=0;i<3;i++) 20 { 21 //使用join後主線程等待執行完之後才結束 22 //threads[i].join(); 23 //脫離主線程,主線程結束不報錯,自動退出 24 threads[i].detach(); 25 } 26
27 Sleep(3000); 28 }

26.多線程join detach