事件对象

今天来总结下事件对象的使用方法

事件对象也属于内核对象,包含一个使用计数,一个用于指明该事件是一个自动重置的事件还是一个人工重置的事件的布尔值,另一个用于指明该事件处于已通知状态还是未通知状态的布尔值。

 

先看MSDN上对创建事件对象的说明

CreateEvent

The CreateEvent function creates or opens a named or unnamed event object.

HANDLE CreateEvent(

  LPSECURITY_ATTRIBUTES lpEventAttributes,

  BOOL bManualReset,

  BOOL bInitialState,

  LPCTSTR lpName

);

The handle returned by CreateEvent has the EVENT_ALL_ACCESS access right and can be used in any function that requires a handle to an event object. For more information, see Synchronization Object Security and Access Rights.

Any thread of the calling process can specify the event-object handle in a call to one of the wait functions. The single-object wait functions return when the state of the specified object is signaled. The multiple-object wait functions can be instructed to return either when any one or when all of the specified objects are signaled. When a wait function returns, the waiting thread is released to continue its execution.

The initial state of the event object is specified by the bInitialState parameter. Use the SetEvent function to set the state of an event object to signaled. Use the ResetEvent function to reset the state of an event object to nonsignaled.

When the state of a manual-reset event object is signaled, it remains signaled until it is explicitly reset to nonsignaled by the ResetEvent function. Any number of waiting threads, or threads that subsequently begin wait operations for the specified event object, can be released while the object's state is signaled.

When the state of an auto-reset event object is signaled, it remains signaled until a single waiting thread is released; the system then automatically resets the state to nonsignaled. If no threads are waiting, the event object's state remains signaled.

Multiple processes can have handles of the same event object, enabling use of the object for interprocess synchronization. The following object-sharing mechanisms are available:

我就不翻译了

结合例子说下大致意思就行了

第一个参数讲安全属性的 一般设置为默认安全属性就行了

第二个参数是设置人工对象还是系统自动重置的对象,当人工重置的事件得到通知时,等待该事件的所有线程均变为可调度线程。当一个自动重置的事件得到通知时,等待该事件的线程中只有一个线程变为可调度线程。

第三个参数表示在创建事件对象的时候是否设置对象的信号状态。

第四个参数 对象名字,可以不设置

 

下面试线程同步的代码

#include <windows.h>

#include <iostream.h>

 

DWORD WINAPI Fun1Proc(

  LPVOID lpParameter   // thread data

);

 

DWORD WINAPI Fun2Proc(

  LPVOID lpParameter   // thread data

);

 

int tickets=100;

HANDLE g_hEvent;

 

void main()

{

       HANDLE hThread1;

       HANDLE hThread2;

       hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);

       hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);

       CloseHandle(hThread1);

       CloseHandle(hThread2);

 

       //g_hEvent=CreateEvent(NULL,TURE,FALSE,NULL);//当使用人工重置的对象同步时会出现问题,

                                                    //因为所有的线程都是通知状态

       g_hEvent=CreateEvent(NULL,FALSE,FALSE,"tickets");

       if(g_hEvent)

       {

              if(ERROR_ALREADY_EXISTS==GetLastError())

              {

                     cout<<"only instance can run!"<<endl;

                     return;

              }

       }

       SetEvent(g_hEvent);

 

       Sleep(4000);

       CloseHandle(g_hEvent);

}

 

DWORD WINAPI Fun1Proc(

  LPVOID lpParameter   // thread data

)

{

       while(TRUE)

       {

              WaitForSingleObject(g_hEvent,INFINITE);

//            ResetEvent(g_hEvent);

              if(tickets>0)

              {

                     Sleep(1);

                     cout<<"thread1 sell ticket : "<<tickets--<<endl;

              }

              else

                     break;

              SetEvent(g_hEvent);

       }

      

       return 0;

}

 

DWORD WINAPI Fun2Proc(

  LPVOID lpParameter   // thread data

)

{

      

       while(TRUE)

       {

              WaitForSingleObject(g_hEvent,INFINITE);

//            ResetEvent(g_hEvent);

              if(tickets>0)

              {

                     Sleep(1);

                     cout<<"thread2 sell ticket : "<<tickets--<<endl;

              }

              else

                     break;

              SetEvent(g_hEvent);

       }

      

       return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值