CPP 多线程

 1 /* CPP 多线程 */
 2 
 3 #include<iostream>
 4 #include<thread>
 5 #include<windows.h>
 6 #include<vector>
 7 
 8 using namespace std;
 9 using namespace std::this_thread;
10 
11 void msg()
12 {
13     MessageBox(0,"12345","6789",0);
14 }
15 
16 void msgA(int num)
17 {
18     std::cout << get_id() << "  num=   " << num << std::endl;
19 }
20 
21 int main()
22 {
23     // thread::hardware_concurrency(); 检测cpu有多少个线程
24     auto n = thread::hardware_concurrency();
25 
26     std::cout << n << std::endl;
27     
28     // 获取当前线程编号 get_id()
29     std::cout << "thread = " << get_id() << std::endl;
30     
31     
32         thread thread1(msg);// 创建多线程
33         thread thread2(msg);
34         thread1.join();// 调用  开始执行
35         thread2.join();// 调用
36     
37     std::cin.get();
38 }
39 
40 
41 //---------------------------------------------------------
42 
43 void main()
44 {
45     vector<thread *> threads;
46     for (int i=0;i<10 ;i++ )
47     {
48         threads.push_back(new thread(msg));// 创建一个线程
49     }
50 
51     for (auto th: threads)
52     {
53         th->join();
54     }
55 
56     std::cin.get();
57 }
58 
59 //---------------------------------------------------------
60 
61 void main()
62 {
63     vector<thread *> threads;
64 
65     for (int i = 0; i < 10; i++)
66     {
67         threads.push_back(new thread(msgA,i));//创建线程
68     }
69 
70     for (auto th : threads)
71     {
72         th->join();
73     }
74 
75     std::cin.get();
76 }

 

posted on 2015-06-02 14:50  Dragon-wuxl  阅读(243)  评论(0)    收藏  举报

导航