15,473
社区成员




#include <windows.h>
#include <iostream>
using namespace std;
HANDLE h;
ULONG WINAPI f1(LPVOID param)
{
for(int i=0;i<100;i++)
{
int rev=::WaitForSingleObject(h,INFINITE);
if(rev==WAIT_OBJECT_0)
{
// ResetEvent(h);
cout<<"the thread1 is:"<<i<<endl;
SetEvent(h);
}
}
return 0;
}
ULONG WINAPI f2(LPVOID param)
{
for(int i=0;i<100;i++)
{
int rev=::WaitForSingleObject(h,INFINITE);
if(rev==WAIT_OBJECT_0)
{
// ResetEvent(h);
cout<<"the thread2 is:"<<i<<endl;
SetEvent(h);
}
}
return 0;
}
int main(int argc, char* argv[])
{
DWORD id1,id2;
h=CreateEvent(NULL,FALSE,TRUE,"WANGXU");
CreateThread(NULL,0,f1,NULL,0,&id1);
CreateThread(NULL,0,f2,NULL,0,&id2);
Sleep(5000);
return 0;
}
h=CreateEvent(NULL,FALSE,TRUE,"WANGXU");
h=CreateEvent(NULL,FALSE,FALSE,"WANGXU");
ULONG WINAPI f2(LPVOID param)
{
int rev=::WaitForSingleObject(h,INFINITE);
for(int i=0;i<100;i++)
{
if(rev==WAIT_OBJECT_0)
{
// ResetEvent(h);
cout<<"the thread2 is:"<<i<<endl;
}
}
SetEvent(h);
return 0;
}