-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread.cpp
More file actions
41 lines (34 loc) · 1013 Bytes
/
Copy paththread.cpp
File metadata and controls
41 lines (34 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
//据说有返回值的线程函数,其返回值会被忽略
void func(int a, int b, int c)
{
std::this_thread::sleep_for(std::chrono::seconds(3));
cout << a << "--" << b << "--" << c << endl;
}
int main()
{
std::thread t1(func, 1, 2, 3);
cout << "ID=" << t1.get_id() << std::endl;
//t1.join();
//后台执行t2线程,程序退出也不会异常
std::thread t2(func, 2, 3, 4);
cout << "ID=" << t2.get_id() << std::endl;
//t2.join();
//以lambda表达式创建线程
std::thread t4([](int a, int b, int c){
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << a << "--" << b << "--" << c << endl;
}, 4,5,6);
cout << "ID=" << t4.get_id() << std::endl;
t1.join();
cout << "join t1" << endl;
t2.join();
cout << "join t2" << endl;
t4.join();
cout << "join t4" << endl;
//std::thread t3(func, 3, 4, 5);
return 0;
}