事件对象实现线程同步

首先分析要使用到的几个API函数:

This function creates a named or an unnamed event object.

HANDLE CreateEvent(
  LPSECURITY_ATTRIBUTES lpEventAttributes, 
  BOOL bManualReset, 
  BOOL bInitialState, 
  LPTSTR lpName 
); 

Parameters

lpEventAttributes
[in] Ignored. Must be NULL.
如果忽略,必须设置为NULL
bManualReset
[in] Boolean that specifies whether a manual-reset or auto-reset event object is created. If TRUE, then you must use the ResetEvent function to manually reset the state to nonsignaled. If FALSE, the system automatically resets the state to nonsignaled after a single waiting thread has been released.
设置人工设置还是系统自动设置,如果是人工设置(TRUE),就需要调用ResetEvent函数设置状态为无信号状态;如果是系统设置(FALSE),系统会自动在设置一个等待线程释放的信号为无信号状态
bInitialState
[in] Boolean that specifies the initial state of the event object. If TRUE, the initial state is signaled; otherwise, it is nonsignaled.
设置出事化的信号状态,如果TRUE,初始化为有信号状态,否则无;
lpName
[in] Pointer to a null-terminated string that specifies the name of the event object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.

If lpName matches the name of an existing named event object, the bManualReset and bInitialState parameters are ignored because they have already been set by the creation process.

If lpName is NULL, the event object is created without a name.

If lpName matches the name of an existing semaphore, mutex, 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.

还有相关若干函数.

现在使用前面Demo :


#include<windows.h>
#include<iostream.h>
DWORD WINAPI FuncThreadone(LPVOID lpParameter);
DWORD WINAPI FuncThreadTwo(LPVOID lpParameter);

int index=0;
int tickets=100;
HANDLE hMutex;
HANDLE m_hEvent;

void main(){
 HANDLE hThread1;
 HANDLE hThread2;
 hMutex=CreateMutex(NULL,0,NULL);
 m_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);

 SetEvent(m_hEvent);

 hThread1=CreateThread(NULL,0,FuncThreadone,NULL,0,NULL);
 hThread2=CreateThread(NULL,0,FuncThreadTwo,NULL,0,NULL);
 CloseHandle(hThread1);
 CloseHandle(hThread2);
 cout<<"main thread is running"<<endl;
 //Sleep(10);
 Sleep(4000);
 CloseHandle(m_hEvent);
}

DWORD WINAPI FuncThreadone(LPVOID lpParameter){

 //cout<<"thread one is running !"<<endl;
 while(1){
  //WaitForSingleObject(hMutex,INFINITE);
  WaitForSingleObject(m_hEvent,INFINITE);
  //ResetEvent(m_hEvent);
  if(tickets>0){
   Sleep(1);
   cout<<"thread1 sell ticket : "<<tickets--<<endl;
   SetEvent(m_hEvent);
  }else{
   SetEvent(m_hEvent);
   break;
  }
  //ReleaseMutex(hMutex);
 }
 return 0;

}

DWORD WINAPI FuncThreadTwo(LPVOID lpParameter){

 //cout<<"thread two is running !"<<endl;
 while(1){
  //WaitForSingleObject(hMutex,INFINITE);
  WaitForSingleObject(m_hEvent,INFINITE);
  //ResetEvent(m_hEvent);
  if(tickets>0){
   Sleep(1);
   cout<<"thread2 sell ticket : "<<tickets--<<endl;
   SetEvent(m_hEvent);
  }else{
   SetEvent(m_hEvent);
   break;
  }
  //ReleaseMutex(hMutex);
 }
 return 0;

}

 

既可以得到运行结果.

上面main函数中对应的两句可以改为:

m_hEvent=CreateEvent(NULL,FALSE,TRUE,NULL);

 //SetEvent(m_hEvent);

 

posted @ 2013-05-02 16:50  MMLoveMeMM  阅读(278)  评论(0)    收藏  举报