Wince线程优先级设定

来源:互联网 发布:2010年韩国进出口数据 编辑:程序博客网 时间:2024/05/17 23:04

wince下设定线程的优先级一般有以下两种方式:

1. 通过ceSetThreadPriority函数

 

BOOL CeSetThreadPriority(

  HANDLE hThread,

  int nPriority

);

第一个参数是线程句柄,第二个值介于 0-255 之间,用于描述所需的优先级。选择使用哪个线程优先级非常关键,而能够以图表表现应用程序优先级的使用,也有助于确保适当的性能。优先级从 0 至 247 的线程(0 表示最高优先级)是实时线程优先级,需要调用 CeSetThreadPriority 来访问。一般线程优先级介于 248-255 之间,要使用 SetThreadPriority 进行访问。下表提供了 Windows CE .NET 标准优先级实现的快速指南。

 

表 3:实时线程优先级:CeSetThreadPriority

优先级组件0-19开放 – 高于驱动程序的实时20Permedia 垂直折返21-98开放 – 高于驱动程序的实时99电源管理恢复线程100-108USB OHCI UHCI、串行109-129Irsir1、NDIS、触摸板130KITL131VMini132CxPort133-144开放 – 设备驱动程序145PS2 键盘146-147开放 – 设备驱动程序148IRComm149开放 – 设备驱动程序150TAPI151-152开放 – 设备驱动程序153-247开放 – 低于驱动程序的实时

表 4:一般线程优先级:SetThreadPriority

优先级组件248电源管理249WaveDev、TVIA5000、鼠标、PnP、电源250WaveAPI251电源管理器电池线程252-255开放

一般来说,最先需要决定的是要确定关键线程是否需要驱动程序。如果关键线程需要驱动程序才能正常工作,而将它的优先级设定为高于驱动程序的优先级,则很难获得好的性能。总之,时间关键型应用程序需要放在“高于驱动程序类别的实时”类别中,优先级范围为 0-98。

 

优先级范围分配对象0-96高于驱动程序的程序97-152基于Windows CE的驱动程序153-247低于驱动程序的程序248-255普通的应用程序

 

 

THREAD_PRIORITY_TIME_CRITICAL         Indicates 3 points above normal priority. 

THREAD_PRIORITY_HIGHEST                    Indicates 2 points above normal priority. 

THREAD_PRIORITY_ABOVE_NORMAL      Indicates 1 point above normal priority. 

THREAD_PRIORITY_NORMAL                    Indicates normal priority. 

THREAD_PRIORITY_BELOW_NORMAL      Indicates 1 point below normal priority. 

THREAD_PRIORITY_LOWEST                    Indicates 2 points below normal priority. 

THREAD_PRIORITY_ABOVE_IDLE             Indicates 3 points below normal priority. 

THREAD_PRIORITY_IDLE                           Indicates 4 points below normal priority. 

 

获取线程优先级:

 

int CeGetThreadPriority(

  HANDLE hThread

);

 

2. 通过SetThreadPriority函数,其设定优先级的范围为:248-255,这个要注意了

 

BOOL SetThreadPriority(
HANDLE hThread, // handle to the thread
int nPriority // thread priority level
);
功能说明
设置指定线程的优先级
参数说明

 

线程优先级等级
标志
优先级值
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


注:当在调试程序时,遇到了莫名其妙的问题,但是又不能发现问题之所在,那可以尝试一下调整线程的优先级,有的时候很好使啊,亲身体会的。

0 0