#include "stdafx.h"
#include <windows.h>
#include "iostream"
using namespace std;
int tickets = 100;
HANDLE hEvent;
DWORD WINAPI func1(LPVOID lpParameter)
{
while(true)
{
WaitForSingleObject(hEvent,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"Thread1 sells tickets"<<tickets--<<endl;
}
else
break;
SetEvent(hEvent);
}
return 0;
}
DWORD WINAPI func2(LPVOID lpParameter)
{
while(true)
{
WaitForSingleObject(hEvent,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"Thread2 sells tickets"<<tickets--<<endl;
}
else
{
break;
}
SetEvent(hEvent);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
hEvent= CreateEvent(NULL,false,true,NULL);
HANDLE hThread1 = CreateThread(NULL,0,func1,NULL,0,NULL);
HANDLE hThread2 = CreateThread(NULL,0,func2,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
Sleep(4000);
CloseHandle(hEvent);
return 0;
}