-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
65 lines (59 loc) · 1.45 KB
/
demo.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
////
//// Created by xwwwb on 2023/5/3.
////
//#include <chrono>
//#include <iostream>
//#include <string>
//#include <thread>
//#include <windows.h>
//using namespace std;
//
//void demo() {
// string text = "this is demo";
// while (true) {
// cout << text << endl;
// this_thread::sleep_for(chrono::seconds(1));
// }
//}
//int main() {
// // 创建线程
// thread t(demo);
// HANDLE hThread = t.native_handle();
// this_thread::sleep_for(chrono::seconds(2));
//// // 挂起线程
//// cout<<"挂起线程"<<endl;
//// SuspendThread(t.native_handle());
//// this_thread::sleep_for(chrono::seconds(5));
//// // 恢复线程
//// cout<<"恢复线程"<<endl;
//// ResumeThread(t.native_handle());
//// this_thread::sleep_for(chrono::seconds(5));
// // 终止线程
// cout<<"终止线程"<<endl;
//
// TerminateThread(hThread, 0);
//
// this_thread::sleep_for(chrono::seconds(2));
// return 0;
//}
#include <iostream>
#include <thread>
#include <chrono>
#include <Windows.h>
void thread_func()
{
while (true)
{
std::cout << "Thread is running..." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
int main()
{
std::thread my_thread(thread_func);
// 让主线程等待一定时间
std::this_thread::sleep_for(std::chrono::seconds(3));
my_thread.~thread();
std::this_thread::sleep_for(std::chrono::seconds(3));
return 0;
}