A usage instance of the C++ thread priority SetThreadPriority
- 2020-04-02 02:48:08
- OfStack
This article illustrates the use of SetThreadPriority for C++ threads. Specific methods are as follows:
//Threadpriority.cpp: defines the entry point for the console application. & have spent < br / >
//
#include "stdafx.h"
#include <Windows.h>
DWORD WINAPI ThreadProcIdle(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread IDLE...n");
}
return 0;
}
DWORD WINAPI ThreadProcNormal(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread Normal...n");
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwThreadIdIdle;
DWORD dwThreadIdNormal;
HANDLE hThread[2];
//Start two threads & NBSP; < br / >
hThread[0] = ::CreateThread(NULL,0, ThreadProcIdle, NULL, CREATE_SUSPENDED, &dwThreadIdIdle);
::SetThreadPriority(hThread[0],THREAD_PRIORITY_IDLE);
::ResumeThread(hThread[0]);
hThread[1] = ::CreateThread(NULL,0, ThreadProcNormal, NULL, CREATE_SUSPENDED, &dwThreadIdNormal);
::SetThreadPriority(hThread[1],THREAD_PRIORITY_NORMAL);
::ResumeThread(hThread[1]);
//Waiting for two threads to end & NBSP; < br / >
::WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
::CloseHandle(hThread[0]);
::CloseHandle(hThread[1]);
return 0;
}
Hope that the article described in the C++ programming to help you.