1 hardware_concurrency();
2 join();
1 #include <iostream> 2 #include <thread> 3 #include <windows.h> 4 5 using namespace std; 6 7 void msg() 8 { 9 MessageBoxA(0, "12345", "678910", 0);//弹出对话框 10 } 11 12 void main() 13 { 14 auto n = thread::hardware_concurrency(); 15 16 std::cout << n << std::endl; 17 18 std::cout << "thread=" << std::this_thread::get_id() << std::endl;//获取当前线程编号 19 20 thread thread1(msg);//创建多线程 21 thread thread2(msg); 22 thread1.join();//开始执行,同时弹出2个对话框 23 thread2.join(); 24 25 system("pause"); 26 }
数组存放进程
1 #include <iostream> 2 #include <thread> 3 #include <vector> 4 #include <windows.h> 5 6 using namespace std; 7 8 void msg() 9 { 10 MessageBoxA(0, "12345", "678910", 0);//弹出对话框 11 } 12 13 void msgA(int num) 14 { 15 std::cout << std::this_thread::get_id() << " num=" << num << std::endl; 16 MessageBoxA(0, "12345", "678910", 0);//弹出对话框 17 } 18 19 void main() 20 { 21 vector<thread *>threads; 22 23 //for (int i = 0; i < 10; i++) 24 //{ 25 // threads.push_back(new thread(msg));//创建线程 26 //} 27 28 for (int i = 0; i < 10; i++) 29 { 30 threads.push_back(new thread(msgA, i));//创建线程 31 } 32 33 for (auto th : threads) 34 { 35 th->join(); 36 } 37 38 system("pause"); 39 }
时间: 2024-10-12 06:47:34