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 }