利用事件对象实现线程同步

不废话,上代码

#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.

 

转载于:https://www.cnblogs.com/yuzhould/archive/2010/01/13/4455030.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值