#include<windows.h>
#include<iostream.h>


DWORD WINAPI Fun1Proc(LPVOID lpParameeter);
DWORD WINAPI Fun2Proc(LPVOID lpParameeter);


int tickets=100;
HANDLE g_hEvent;


void main()
{
	HANDLE hThread1;
	HANDLE hThread2;

	g_hEvent=CreateEvent(NULL,FALSE,FALSE,"tickets");
	if(g_hEvent)
	{
		if(ERROR_ALREADY_EXISTS==GetLastError())
		{
			cout<<"only one "<<endl;
			return ;
		}
	}

	SetEvent(g_hEvent);
	hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
	hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);

	CloseHandle(hThread1);
	CloseHandle(hThread2);

	Sleep(400);

	CloseHandle(g_hEvent);

}


DWORD WINAPI Fun1Proc(LPVOID lpParameeter)
{
	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 lpParameeter)
{
	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;
}