SetThreadPriority 設置指定線程的優先級
[cpp]
BOOL SetThreadPriority( HANDLE hThread, // handle to the thread
int nPriority // thread priority level);
參數說明
hThread 要設置的線程句柄
nPriority 優先級別參數 可設置為一下參數
THREAD_PRIORITY_ABOVE_NORMAL 比一般優先級高一個等級
THREAD_PRIORITY_BELOW_NORMAL 比一般低一個等級
THREAD_PRIORITY_HIGHEST 比一般高2個等級(最高)
THREAD_PRIORITY_IDLE 空閒
THREAD_PRIORITY_LOWEST 比一般低2個等級(最低)
THREAD_PRIORITY_NORMAL 一般等級
THREAD_PRIORITY_TIME_CRITICAL 實時
[cpp]
#include <Windows.h>
#include <iostream>
using namespace std;
DWORD WINAPI ThreadNormal(LPVOID lpParam)
{
int i=0;
while(i++ < 10)
cout<<"Normal Thread is running"<<endl;
return 0;
}
DWORD WINAPI ThreadAboveNormal(LPVOID lpParam)
{
int i=0;
while(i++ < 10)
cout<<"Above Normal Thread is running"<<endl;
return 0;
}
int main()
{
DWORD dwThreadID;
HANDLE hThread[2];
//Create a above Normal Thread
hThread[0] = CreateThread(NULL,0,ThreadAboveNormal,NULL,CREATE_SUSPENDED,&dwThreadID);
//set priority is above normal www.2cto.com
SetThreadPriority(hThread[0],THREAD_PRIORITY_ABOVE_NORMAL);
//resume thread
ResumeThread(hThread[0]);
//Create a Normal Thread
hThread[1] = CreateThread(NULL,0,ThreadNormal,NULL,0,&dwThreadID);
//wait two
WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
//close thread
CloseHandle(hThread[0]);
CloseHandle(hThread[1]);
return 0;
}