include "stdafx.h"
include "MyEvent.h"
ifdef _DEBUG
define new DEBUG_NEW
undef THIS_FILE
static char THIS_FILE[] = __FILE__;
endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
DWORD threadA(void* pD)
{
int iID=(int)pD;
//ÔÚÄÚ²¿ÖØдò¿ª
HANDLE hCounterIn=OpenEvent(EVENT_ALL_ACCESS,FALSE,"My Try");
printf("\tthread %d begin\n",iID);
//ÉèÖóÉΪÓÐÐźÅ״̬
Sleep(1000);
SetEvent(hCounterIn);
Sleep(1000);
printf("\tthread %d end\n",iID);
CloseHandle(hCounterIn);
return 0;
}
DWORD threadB(void* pD)
{
//µÈ´ýthreadA½áÊøºóÔÙ¼ÌÐøÖ´ÐÐ
int iID=(int)pD;
//ÔÚÄÚ²¿ÖØдò¿ª
HANDLE hCounterIn=OpenEvent(EVENT_ALL_ACCESS,FALSE,"My Try");
if(WAIT_TIMEOUT == WaitForSingleObject(hCounterIn,10*1000))
{
printf("\t\tthread %d wait time out\n",iID);
}
else
{
printf("\t\tthread %d wait ok\n",iID);
}
CloseHandle(hCounterIn);
return 0;
}
void main()
{
HANDLE hCounter=NULL;
if( (hCounter=OpenEvent(EVENT_ALL_ACCESS,FALSE,"My Try"))==NULL)
{
//Èç¹ûûÓÐÆäËû½ø³Ì´´½¨Õâ¸öʼþ£¬ÔòÖØд´½¨£¬¸ÃʼþΪÈ˹¤ÖØÖÃʼþ
hCounter = CreateEvent(NULL,TRUE,FALSE,"My Try");
}
//´´½¨Ïß³Ì
HANDLE hThread[3];
printf("test of manual rest event\n");
CWinThread* pT1=AfxBeginThread((AFX_THREADPROC)threadA,(void*)1);
CWinThread* pT2=AfxBeginThread((AFX_THREADPROC)threadB,(void*)2);
CWinThread* pT3=AfxBeginThread((AFX_THREADPROC)threadB,(void*)3);
hThread[0]=pT1->m_hThread;
hThread[1]=pT2->m_hThread;
hThread[2]=pT3->m_hThread;
//µÈ´ýÏ߳̽áÊø
WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
//¹Ø±Õ¾ä±ú
CloseHandle(hCounter);
if( (hCounter=OpenEvent(EVENT_ALL_ACCESS,FALSE,"My Try"))==NULL)
{
//Èç¹ûûÓÐÆäËû½ø³Ì´´½¨Õâ¸öʼþ£¬ÔòÖØд´½¨£¬¸ÃʼþΪ×Ô¶¯ÖØÖÃʼþ
hCounter = CreateEvent(NULL,FALSE,FALSE,"My Try");
}
//´´½¨Ïß³Ì
printf("test of auto rest event\n");
pT1=AfxBeginThread((AFX_THREADPROC)threadA,(void*)1);
pT2=AfxBeginThread((AFX_THREADPROC)threadB,(void*)2);
pT3=AfxBeginThread((AFX_THREADPROC)threadB,(void*)3);
hThread[0]=pT1->m_hThread;
hThread[1]=pT2->m_hThread;
hThread[2]=pT3->m_hThread;
//µÈ´ýÏ߳̽áÊø
WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
//¹Ø±Õ¾ä±ú
CloseHandle(hCounter);
}
来源:PUDN