利用事件对象实现线程同步
不废话,上代码
#include<windows.h>
#include<iostream>
using namespace std;
DWORD WINAPI fun1proc(LPVOID lpParameter);
DWORD WINAPI fun2proc(LPVOID lpParameter);
int index=0;
int tickets=100;
HANDLE g_hEvent;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
g_hEvent=CreateEvent(NULL,FALSE,FALSE,"tickets"); //创建事件对象
//保证应用程序只有一个实例运行
if(g_hEvent)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only one instance can run!"<<endl;
int a;
scanf("%d",&a);
return ;
}
}
SetEvent(g_hEvent);
hThread1=CreateThread(NULL,0,fun1proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,fun2proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
Sleep(4000);
CloseHandle(g_hEvent);
}
DWORD WINAPI fun1proc(LPVOID lpParameter)
{
while(1)
{
WaitForSingleObject(g_hEvent,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket"<<tickets--<<endl;
SetEvent(g_hEvent); //一个线程函数运行完毕后使事件对象处于有信号状态。
}
else
break;
}
return 0;
}
DWORD WINAPI fun2proc(LPVOID lpParameter)
{
while(1)
{
WaitForSingleObject(g_hEvent,INFINITE);
ResetEvent(g_hEvent);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket"<<tickets--<<endl;
SetEvent(g_hEvent);
}
else
break;
}
return 0;
}
小解释:
CreateEventA(
__in_opt LPSECURITY_ATTRIBUTES lpEventAttributes,
__in BOOL bManualReset,
__in BOOL bInitialState,
__in_opt LPCSTR lpName
);
#define CreateEvent CreateEventA
在程序中可以通过CreateEvent函数创建或打开一个命名的或匿名的事件对象,该函数的原型如上。该函数的四个参数下解释
Parameters
lpEventAttributes
A pointer to a SECURITY_ATTRIBUTES structure. If this parameter is NULL, the handle cannot be inherited by child processes.
The lpSecurityDescriptor member of the structure specifies a security descriptor for the new event. If lpEventAttributes is NULL, the event gets a default security descriptor. The ACLs in the default security descriptor for an event come from the primary or impersonation token of the creator.
bManualReset
If this parameter is TRUE, the function creates a manual-reset event object, which requires the use of the ResetEvent function to set the event state to nonsignaled. If this parameter is FALSE, the function creates an auto-reset event object, and system automatically resets the event state to nonsignaled after a single waiting thread has been released.
bInitialState
If this parameter is TRUE, the initial state of the event object is signaled; otherwise, it is nonsignaled.
lpName
The name of the event object. The name is limited to MAX_PATH characters. Name comparison is case sensitive.
If lpName is NULL, the event object is created without a name.
If lpName matches the name of an existing semaphore, mutex, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.
Return Value
If the function succeeds, the return value is a handle to the event object. If the named event object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器