int index=0;
int tickets=100;
HANDLE hMutex;
HANDLE hMutex2;
DWORD WINAPI Fun1Proc(LPVOID lpParamter)
{
hMutex2=CreateMutex(NULL,TRUE,"miao");
if(hMutex2)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"mutex2 is aleday exist Func1proc"<<endl;
}
else
{
cout<<"hMutex2 is created by fun1proc"<<endl;
}
}
while(true)
{
ReleaseMutex(hMutex2);
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex2,INFINITE);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell tickets:"<<tickets--<<endl;
// SetEvent(g_hEvent);
//The state of an auto-reset event object remains signaled until a single waiting thread is released,
//at which time the system automatically sets the state to nonsignaled.
//If no threads are waiting, the event object's state remains signaled.
}
else
{
// SetEvent(g_hEvent);
break;
}
ReleaseMutex(hMutex);
ReleaseMutex(hMutex2);
}
return 0;
}