线程的优先级别会自动提高吗?进程a.exe 有一个线程 a_thread,它的级别为THREAD_PRIORITY_NORMAL
进程b.exe 有一个线程 b_thread,它的级别为THREAD_PRIORITY_BELOW_NORMAL两个进程等级(Process Priority Class)一样,显然b_thread的优先级别是比a_thread低。那么问题是:
当a.exe和b.exe都启动(a_thread和b_thread也启动了)以后,
但是b.exe在启动b_thread以后,再设置了进程等级为HIGH_PRIORITY_CLASS,请问
b_thread的级别会自动随着进程等级的提高而提高吗?而变成b_thread > a_thread吗?(注:)
The priority of each thread is determined by the following criteria:    * The priority class of its process
    * The priority level of the thread within the priority class of its process
谢谢。
附录MSSDN的参考文章:
Scheduling PrioritiesThreads are scheduled to run based on their scheduling priority. Each thread is 
assigned a scheduling priority. The priority levels range from zero (lowest 
priority) to 31 (highest priority). Only the zero-page thread can have a 
priority of zero. (The zero-page thread is a system thread responsible for 
zeroing any free pages when there are no other threads that need to run.)The system treats all threads with the same priority as equal. The system 
assigns time slices in a round-robin fashion to all threads with the highest 
priority. If none of these threads are ready to run, the system assigns time 
slices in a round-robin fashion to all threads with the next highest priority. 
If a higher-priority thread becomes available to run, the system ceases to 
execute the lower-priority thread (without allowing it to finish using its time 
slice), and assigns a full time slice to the higher-priority thread. For more 
information, see Context Switches.The priority of each thread is determined by the following criteria:    * The priority class of its process
    * The priority level of the thread within the priority class of its processThe priority class and priority level are combined to form the base priority of 
a thread. For information on the dynamic priority of a thread, see Priority 
Boosts.Priority ClassEach process belongs to one of the following priority classes:IDLE_PRIORITY_CLASS
BELOW_NORMAL_PRIORITY_CLASS
NORMAL_PRIORITY_CLASS
ABOVE_NORMAL_PRIORITY_CLASS
HIGH_PRIORITY_CLASS
REALTIME_PRIORITY_CLASS    Windows NT:  BELOW_NORMAL_PRIORITY_CLASS and ABOVE_NORMAL_PRIORITY_CLASS 
are not supported.By default, the priority class of a process is NORMAL_PRIORITY_CLASS. Use the 
CreateProcess function to specify the priority class of a child process when 
you create it. If the calling process is IDLE_PRIORITY_CLASS or 
BELOW_NORMAL_PRIORITY_CLASS, the new process will inherit this class. Use the 
GetPriorityClass function to determine the current priority class of a process 
and the SetPriorityClass function to change the priority class of a process.Processes that monitor the system, such as screen savers or applications that 
periodically update a display, should use IDLE_PRIORITY_CLASS. This prevents 
the threads of this process, which do not have high priority, from interfering 
with higher priority threads.Use HIGH_PRIORITY_CLASS with care. If a thread runs at the highest priority 
level for extended periods, other threads in the system will not get processor 
time. If several threads are set at high priority at the same time, the threads 
lose their effectiveness. The high-priority class should be reserved for 
threads that must respond to time-critical events. If your application performs 
one task that requires the high-priority class while the rest of its tasks are 
normal priority, use SetPriorityClass to raise the priority class of the 
application temporarily; then reduce it after the time-critical task has been 
completed. Another strategy is to create a high-priority process that has all 
of its threads blocked most of the time, awakening threads only when critical 
tasks are needed. The important point is that a high-priority thread should 
execute for a brief time, and only when it has time-critical work to perform.You should almost never use REALTIME_PRIORITY_CLASS, because this interrupts 
system threads that manage mouse input, keyboard input, and background disk 
flushing. This class can be appropriate for applications that "talk" directly 
to hardware or that perform brief tasks that should have limited interruptions.Priority LevelThe following are priority levels within each priority classTHREAD_PRIORITY_IDLE
THREAD_PRIORITY_LOWEST
THREAD_PRIORITY_BELOW_NORMAL
THREAD_PRIORITY_NORMAL
THREAD_PRIORITY_ABOVE_NORMAL
THREAD_PRIORITY_HIGHEST
THREAD_PRIORITY_TIME_CRITICALAll threads are created using THREAD_PRIORITY_NORMAL. This means that the 
thread priority is the same as the process priority class. After you create a 
thread, use the SetThreadPriority function to adjust its priority relative to 
other threads in the process.A typical strategy is to use THREAD_PRIORITY_ABOVE_NORMAL or 
THREAD_PRIORITY_HIGHEST for the process's input thread, to ensure that the 
application is responsive to the user. Background threads, particularly those 
that are processor intensive, can be set to THREAD_PRIORITY_BELOW_NORMAL or 
THREAD_PRIORITY_LOWEST, to ensure that they can be preempted when necessary. 
However, if you have a thread waiting for another thread with a lower priority 
to complete some task, be sure to block the execution of the waiting 
high-priority thread. To do this, use a wait function, critical section, or the 
Sleep function, SleepEx, or SwitchToThread function. This is preferable to 
having the thread execute a loop. Otherwise, the process may become deadlocked, 
because the thread with lower priority is never scheduled.To determine the current priority level of a thread, use the GetThreadPriority 
function.Base PriorityThe priority level of a thread is determined by both the priority class of its 
process and its priority level. The priority class and priority level are 
combined to form the base priority of each thread.

解决方案 »

  1.   

    线程优先级 = 进程优先级类 + 线程相对优先级
    进程与线程的优先级
    每个进程和线程都有相应的优先级设置,线程的优先级决定它何时运行和接收
    多少CPU时间。最终的优先级共32级,是从0到31的数值,称为基本优先级别(base 
    priority level)。系统按照不同的优先级调度线程的运行。
    0-15级是普通优先级,线程的优先级可以动态变化,高优先级线程优先运行,
    只有高优先级线程不运行时,才调度低优先级线程运行。优先级相同的线程按照时
    间片轮流运行。
    16-31级是实时优先级,实时优先级与普通优先级的最大区别在于相同优先级线
    程的运行不按照时间片轮转,而是先运行的线程就先控制CPU,如果它不主动放弃
    控制,同级或低优先级的线程就无法运行。
    线程的实际优先级设置是两个值的结合:所属的进程的总优先级类和线程本身
    的优先级别。一个线程的优先级首先属于一个类,然后是其在该类中的相对位置。
    即:
            线程优先级 = 进程优先级类 + 线程相对优先级
    进程的优先级类从高到低如下(数字为基本优先级别数量):
            REALTIME_PRIORITY_CLASS 24
            HIGH_PRIORITY_CLASS                     13
            NORMAL_PRIORITY_CLASS           前台为9,后台为7
            IDLE_PRIORITY_CLASS                     4
    进程的相对优先级:
    THREAD_PRIORITY_LOWEST                          比所在进程优先级低两个级别
    THREAD_PRIORITY_BELOW_NORMAL    比所在进程优先级低一个级别
    THREAD_PRIORITY_NORMAL                  与进程同级
    THREAD_PRIORITY_ABOVE_NORMAL    比所在进程优先级高一个级别
    THREAD_PRIORITY_HIGHEST                         比所在进程优先级高两个级别
    HREAD_PRIORITY_IDLE                                     基本优先级为1。对于
    REALTIME_PRIORITY_CLASS进程,优先级为16
    THREAD_PRIORITY_TIME_CRITICAL     基本优先级为15。对于                       
         
    REALTIME_PRIORITY_CLASS进程,优先级别是31。
    在一般情况下,进程和线程的优先级被设置成NORMAL_PRIORITY_CLASS和
    THREAD_PRIORITY_NORMAL,后面可以看到,程序运行时可以获取和更改进程
    或线程的优先级。
      

  2.   

    The priority level of a thread is determined by both the priority class of its 
    process and its priority level. The priority class and priority level are 
    combined to form the base priority of each thread.
      

  3.   

    怎么大家都正面回答我的问题啊?
    一个是翻译(我认识外文,我只是帖一下给大家参考),
    一个是把我知道的,从新说了一遍 -_-当然了,老实说,这个问题可能也难了一点,所以如果过几天没人有更好更直接的答案
    我就给你们两个各30分,哈哈。还是要谢谢的。我的问题:
    线程的优先级别会自动提高吗?进程a.exe 有一个线程 a_thread,它的级别为THREAD_PRIORITY_NORMAL
    进程b.exe 有一个线程 b_thread,它的级别为THREAD_PRIORITY_BELOW_NORMAL两个进程等级(Process Priority Class)一样,显然b_thread的优先级别是比a_thread低。那么问题是:
    当a.exe和b.exe都启动(a_thread和b_thread也启动了)以后,
    但是b.exe在启动b_thread以后,再设置了进程等级为HIGH_PRIORITY_CLASS,请问
    b_thread的级别会自动随着进程等级的提高而提高吗?而变成b_thread > a_thread吗?
      

  4.   

    我猜答案是:YESb_thread > a_thread前面两位说的还是对的,跟我想的一样。