CreateEvent(NULL,TRUE,FALSE,NULL);的解释

本文详细介绍了如何使用CreateEvent函数创建命名或无名的事件对象,包括事件对象的继承性、复原方式、初始状态和命名规则。通过实例展示了事件对象在多线程或多进程环境中的应用。

它用来创建或打开一个命名的或无名的事件对象
初始化为:
m_hReadEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
1.,确定返回的句柄是否可被子进程继承.如果lpEventAttributes是NULL,此句柄不能被继承。
2.指定将事件对象创建成手动复原还是自动复原。如果是TRUE,那么必须用ResetEvent函数来手工将事件的状态复原到无信号状态。如果设置为FALSE,当一个等待线程被释放以后,系统将会自动将事件状态复原为无信号状态。
3.指定事件对象的初始状态。如果为TRUE,初始状态为有信号状态;否则为无信号状态。
4.指定事件的对象的名称,

#include <windows.h> #include <iostream> using namespace std; const int N = 5; // 进程数 int count = 0; // 计数器 HANDLE mutex = CreateMutex(NULL, FALSE, NULL); // 互斥量 HANDLE barrier = CreateEvent(NULL, TRUE, FALSE, NULL); // 屏障 DWORD WINAPI Process(LPVOID lpParam) { int id = ((int)lpParam); cout << "Process " << id << " arrived at barrier." << endl; WaitForSingleObject(mutex, INFINITE); count++; ReleaseMutex(mutex); if (count == N) { cout << "All processes arrived at barrier, releasing barrier." << endl; SetEvent(barrier); } WaitForSingleObject(barrier, INFINITE); cout << "Process " << id << " starts the next phase of work." << endl; return 0; } DWORD WINAPI Broadcast(LPVOID lpParam) { WaitForSingleObject(mutex, INFINITE); cout << "Broadcast process started." << endl; ReleaseMutex(mutex); SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); WaitForSingleObject(barrier, INFINITE); cout << "Broadcast process releasing all processes." << endl; ReleaseMutex(mutex); for (int i = 0; i < N; i++) { ReleaseSemaphore((HANDLE)lpParam, 1, NULL); } return 0; } int main() { HANDLE threads[N]; DWORD threadIds[N]; HANDLE sem = CreateSemaphore(NULL, 0, N, NULL); int ids[N]; for (int i = 0; i < N; i++) { ids[i] = i; threads[i] = CreateThread(NULL, 0, Process, &ids[i], 0, &threadIds[i]); if (threads[i] == NULL) { return 1; } } HANDLE broadcastThread = CreateThread(NULL, 0, Broadcast, sem, 0, NULL); if (broadcastThread == NULL) { return 1; } WaitForMultipleObjects(N, threads, TRUE, INFINITE); WaitForSingleObject(mutex, INFINITE); cout << "All processes completed." << endl; ReleaseMutex(mutex); return 0; }将此代码中的线程替换成进程
06-12
#include<windows.h> #include<iostream> #include<process.h> #include<vector> #include<string> #include<fstream> #include<filesystem> using namespace std; namespace fs = std::filesystem; // 全局变量 vector<string> fileList; // 存储所有文件路径 HANDLE evGetAllFiles, evRead1, evRead2; // 同步事件 // 获取当前目录下所有文件 void GetAllFiles(LPVOID param) { cout << "GetAllFiles is reading all files" << endl; try { for (const auto& entry : fs::directory_iterator(fs::current_path())) { if (entry.is_regular_file() && entry.path().extension() == ".txt") { fileList.push_back(entry.path().string()); } } } catch (const fs::filesystem_error& e) { cerr << "文件系统错误: " << e.what() << endl; } cout << "GetAllFiles have read all files" << endl; // 通知 ReadThread1 可以开始工作 SetEvent(evGetAllFiles); } // 处理奇数索引文件 void ReadThread1(LPVOID param) { // 等待 GetAllFiles 完成 WaitForSingleObject(evGetAllFiles, INFINITE); cout << "\nReadThread1 is reading file:" << endl; string currentFilePath = ""; // 记录当前处理的文件路径 for (size_t i = 0; i < fileList.size(); i += 2) { ifstream file(fileList[i]); if (!file.is_open()) continue; string line; size_t lineNum = 1; // 记录行号 bool hasMatch = false; // 标记文件是否有匹配行 while (getline(file, line)) { if (line.find("abc") != string::npos) { if (!hasMatch) { // 首次匹配时输出文件信息 currentFilePath = fileList[i]; cout << "The file " << currentFilePath << " includes the string abc is:" << endl; hasMatch = true; } cout << "line " << lineNum << ": " << line << endl; } lineNum++; } } // 通知 ReadThread2 可以开始工作 SetEvent(evRead1); } // 处理偶数索引文件 void ReadThread2(LPVOID param) { // 等待 ReadThread1 完成 WaitForSingleObject(evRead1, INFINITE); cout << "\nReadThread2 is reading file:" << endl; string currentFilePath = "C:\\Users\\91643\\Downloads\\text123"; // 记录当前处理的文件路径 for (size_t i = 1; i < fileList.size(); i += 2) { ifstream file(fileList[i]); if (!file.is_open()) continue; string line; size_t lineNum = 1; // 记录行号 bool hasMatch = false; // 标记文件是否有匹配行 while (getline(file, line)) { if (line.find("abc") != string::npos) { if (!hasMatch) { // 首次匹配时输出文件信息 currentFilePath = fileList[i]; cout << "The file " << currentFilePath << " includes the string abc is:" << endl; hasMatch = true; } cout << "line " << lineNum << ": " << line << endl; } lineNum++; } } // 通知主程序可以结束 SetEvent(evRead2); } int main() { // 创建事件对象 evGetAllFiles = CreateEvent(NULL, FALSE, FALSE, NULL); evRead1 = CreateEvent(NULL, FALSE, FALSE, NULL); evRead2 = CreateEvent(NULL, FALSE, FALSE, NULL); // 创建线程 _beginthread(GetAllFiles, 0, NULL); _beginthread(ReadThread1, 0, NULL); _beginthread(ReadThread2, 0, NULL); // 等待所有工作完成 WaitForSingleObject(evRead2, INFINITE); cout << "\nThe Program is End" << endl; // 清理资源 CloseHandle(evGetAllFiles); CloseHandle(evRead1); CloseHandle(evRead2); return 0; }请修改代码使文件查找路径为"C:\Users\91643\Downloads\text123"
最新发布
05-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物联网小镇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值