线程优先级
l 线程优先级
l 获取当前线程句柄
l 线程优先级设置
l 线程优先级变动
l 线程优先级获取
一、线程优先级(Thread priority )
简单的说就是(线程)的优先级越高,那么就可以分占相对多的CPU时间片。 每个进程都有相应的优先级,优先级决定它何时运行和占用 CPU 时间。最终的优先级共分32级,是从 0 到 31 的数值,称为基本优先级别。
进程优先级 priority class |
标志 |
优先级值 |
idle (低) |
IDLE_PRIORITY_CLASS |
4 |
Below 低于标准 |
BELOW_NORMAL_PRIORITY_CLASS |
此值在2000以下系统不支持 |
normal (标准) |
NORMAL_PRIORITY_CLASS |
9(前台) 或 7(后台) |
Above 高于标准 |
ABOVE_NORMAL_PRIORITY_CLASS |
此值在2000以下系统不支持 |
high (高) |
HIGH_PRIORITY_CLASS |
13 |
realtime (实时) |
REALTIME_PRIORITY_CLASS |
24 |
一个线程创建的时候,会继承进程的优先级等级。另外线程优先级可以用SetThreadPriority来进行微调:
微调范围
|
线程优先级等级 |
标志 |
优先级值 |
1 |
idle (最低) |
THREAD_PRIORITY_IDLE |
如果进程优先级为realtime则调整为16,其它情况为1 |
2 |
LOWEST 低 |
THREAD_PRIORITY_LOWEST |
-2(在原有基础上-2) |
3 |
BELOW 低于标准 |
THREAD_PRIORITY_BELOW_NORMAL |
-1(在原有基础上-1) |
4 |
NORMAL(标准) |
THREAD_PRIORITY_NORMAL |
不变(取进程优先级值) |
5 |
ABOVE 高于标准 |
THREAD_PRIORITY_ABOVE_NORMAL |
+1(在原有基础上+1) |
6 |
HIGHEST (高) |
THREAD_PRIORITY_HIGHEST |
+2(在原有基础上+2) |
7 |
CRITICAL(最高) |
THREAD_PRIORITY_TIME_CRITICAL |
如果进程优先级为realtime则调整为31,其它情况为15 |
二、获取当前线程句柄
HANDLE GetCurrentThread(VOID)
//返回当前进句柄
DWORD GetCurrentThreadId(VOID)
//返回当前进程ID值
1、其它线程函数
BOOL SetThreadPriority 设置一个线程的优先级别
int GetThreadPriority 返回一个线程的 优先级别
SetThreadPriorityBoost 设置线程是否禁用动态调整优先级功能。
GetThreadPriorityBoost 获取线程是否禁用了优先级微调功能。
三、线程优先级的设置
BOOL SetThreadPriority
(
HANDLE hThread,// 线程句柄 handle to the
thread
int nPriority // 要设置线程优先级别thread priority level
);
void CDialog_PROCESS::OnBnClickedRadio7()
{
// TODO: 在此添加控件通知处理程序代码
BOOL r=0;
UpdateData(true);
switch (m_radio_thread_selNum)
{
case 0:
r=SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_IDLE); //设置为 最低
TRACE("thread=%d,%04x set priority to THREAD_PRIORITY_IDLE \n",GetCurrentThreadId(),GetCurrentThreadId());
break;
case 1:
r=SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_LOWEST);//设置为 较低
TRACE("thread=%d,%04x set priority to THREAD_PRIORITY_LOWEST \n",GetCurrentThreadId(),GetCurrentThreadId());
break;
case 2:
r=SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_BELOW_NORMAL); //设置为 低于标准
TRACE("thread=%d,%04X set priority to THREAD_PRIORITY_BELOW_NORMAL \n",GetCurrentThreadId(),GetCurrentThreadId());
break;
case 3:
r=SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL); //设置为 标准
TRACE("thread=%d,%04X set priority to THREAD_PRIORITY_NORMAL \n",GetCurrentThreadId(),GetCurrentThreadId());
break;
case 4:
r=SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_ABOVE_NORMAL); //设置为 高于标准
TRACE("thread=%d,%04X set priority to THREAD_PRIORITY_ABOVE_NORMAL \n",GetCurrentThreadId(),GetCurrentThreadId());
break;
case 5:
r=SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST); //设置为 较高
TRACE("thread=%d,%04X set priority to THREAD_PRIORITY_HIGHEST \n",GetCurrentThreadId(),GetCurrentThreadId());
break;
case 6:
r=SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL); //设置为 最高
TRACE("thread=%d,%04X set priority to THREAD_PRIORITY_TIME_CRITICAL \n",GetCurrentThreadId(),GetCurrentThreadId());
break;
default:
break;
}
}