-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathczxt11.cpp
More file actions
47 lines (35 loc) · 1.33 KB
/
czxt11.cpp
File metadata and controls
47 lines (35 loc) · 1.33 KB
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
#include <windows.h>
#include <iostream>
HANDLE hSemaphore;
// 子线程函数
DWORD WINAPI ChildThreadProc(LPVOID lpParameter) {
std::cout << "子线程:开始执行任务..." << std::endl;
Sleep(2000);
std::cout << "子线程:任务执行完毕,准备唤醒父线程" << std::endl;
// 释放信号量,唤醒等待的父线程
ReleaseSemaphore(hSemaphore, 1, NULL);
return 0;
}
int main() {
std::cout << "计算机25-5齐浩凯2404010526" << std::endl;
hSemaphore = CreateSemaphore(NULL, 0, 1, NULL);
if (hSemaphore == NULL) {
std::cerr << "创建信号量失败!错误码:" << GetLastError() << std::endl;
return 1;
}
std::cout << "父线程:创建子线程..." << std::endl;
HANDLE hThread = CreateThread(NULL, 0, ChildThreadProc, NULL, 0, NULL);
if (hThread == NULL) {
std::cerr << "创建子线程失败!错误码:" << GetLastError() << std::endl;
CloseHandle(hSemaphore);
return 1;
}
std::cout << "父线程:等待子线程执行完毕..." << std::endl;
// 等待信号量被释放,进入阻塞状态
WaitForSingleObject(hSemaphore, INFINITE);
std::cout << "父线程:收到子线程通知,继续执行" << std::endl;
// 清理资源
CloseHandle(hThread);
CloseHandle(hSemaphore);
return 0;
}