#include <QCoreApplication>
#include <windows.h>
#include <stdio.h>
#define MAXNUM 10
int g_Ticket = 100;
HANDLE g_aryThread[MAXNUM];
HANDLE M_hMutex = CreateMutexW(
0,
0,
0
);
HANDLE m_hEevnt = CreateEventW(NULL,FALSE,TRUE,0);
HANDLE m_hSemapore = CreateSemaphoreW(
0,
1,
1,
0
);
CRITICAL_SECTION g_cs;
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
while(1)
{
if (g_Ticket<=0)
break;
if (WAIT_TIMEOUT==WaitForSingleObject(m_hSemapore,10))
continue;
if (g_Ticket<=0){
}
printf("current thread is [%d] the ticket number is: %d \n",GetCurrentThreadId(),g_Ticket);
g_Ticket--;
ReleaseSemaphore(m_hSemapore,1,0);
Sleep(10);
}
return 0;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
for(int i = 0; i < MAXNUM; i++)
{
g_aryThread[i] = CreateThread(0,0,&ThreadProc,0,0,0);
}
return a.exec();
}
|