Home > Back-end >  Do a Windows system thread priority, my question is why thread 1 does not perform?
Do a Windows system thread priority, my question is why thread 1 does not perform?

Time:09-24

# include
# include
DWORD WINAPI Fun1Proc (LPVOID lpParameter);
DWORD WINAPI Fun2Proc (LPVOID lpParameter);
DWORD WINAPI Fun3Proc (LPVOID lpParameter);
CRITICAL_SECTION g_cs;
Int tickets=15;
Void main ()
{

HANDLE hThread1;
HANDLE hThread2;
HANDLE hThread3;

HThread1=CreateThread (NULL, 0, Fun1Proc, NULL, CREATE_SUSPENDED, NULL);
SetThreadPriority (hThread1 THREAD_PRIORITY_HIGHEST);

HThread2=CreateThread (NULL, 0, Fun2Proc, NULL, CREATE_SUSPENDED, NULL);
SetThreadPriority (hThread2, THREAD_PRIORITY_NORMAL);

HThread3=CreateThread (NULL, 0, Fun3Proc, NULL, CREATE_SUSPENDED, NULL);
SetThreadPriority (hThread3 THREAD_PRIORITY_IDLE);

ResumeThread (hThread1);
ResumeThread (hThread2);
ResumeThread (hThread3);
The WaitForSingleObject (hThread1, INFINITE);
//SetThreadPriority (hThread3 IDLE_PRIORITY_CLASS);
The CloseHandle (hThread1);
The CloseHandle (hThread2);
The CloseHandle (hThread3);


DeleteCriticalSection (& amp; G_cs);
}
DWORD WINAPI Fun1Proc (LPVOID lpParameter) {
Sleep (20);

While (TRUE)
{
The EnterCriticalSection (& amp; G_cs);
If (tickets> 5)
{
//Sleep (1);
Cout<" Thread1 sell ticket: "& lt; }
The else
break;

LeaveCriticalSection (& amp; G_cs);

}
return 0;
}
DWORD WINAPI Fun2Proc (LPVOID lpParameter) {
InitializeCriticalSection (& amp; G_cs);
While (TRUE)
{

The EnterCriticalSection (& amp; G_cs);
If (tickets> 0)
{
//Sleep (1);
Cout<" Thread2 sell ticket: "& lt; }
The else
break;
If (tickets==5) LeaveCriticalSection (& amp; G_cs);
}
return 0;
}
DWORD WINAPI Fun3Proc (LPVOID lpParameter) {
Cout<" Thread 3 start running!" return 0;
}
Run results
  • Related