码迷,mamicode.com
首页 > 其他好文 > 详细

#include <thread>

时间:2016-06-25 13:42:00      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

 

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 }

 

#include <thread>

标签:

原文地址:http://www.cnblogs.com/denggelin/p/5616213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!