1. 每个线程都有一个优先级,取值为0~31;
2. Windows调度线程的原则:只要高优先级的进程可调度,就不会为低优先级的线程分配CPU;
3. 线程的相对优先级就是线程的真实优先级,与其所在的进程优先级无关。
4. 要改变线程的优先级,要调用函数:
BOOL WINAPI SetThreadPriority( __in HANDLE hThread, __in int nPriority );
程序实例:
#include "stdafx.h"
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
for(int i = 0;i < 10;i++)
cout << "Idle Thread is running"<<endl;
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParam)
{
for(int i = 0;i < 10;i++)
cout << "Normal Thread is running"<<endl;
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE h[2];
DWORD dwThreadID[2];
h[0] = ::CreateThread(NULL,NULL,ThreadProc1,NULL,CREATE_SUSPENDED,&dwThreadID[0]);
h[1] = ::CreateThread(NULL,NULL,ThreadProc2,NULL,CREATE_SUSPENDED,&dwThreadID[0]);
::SetThreadPriority(h[0],THREAD_PRIORITY_IDLE);
::ResumeThread(h[0]);
::ResumeThread(h[1]);
::WaitForMultipleObjects(2,h,TRUE,INFINITE);
system("pause");
return 0;
}