本文介绍了SetThreadPriority和SetPriorityClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用SetThreadPrioritySetPriorityClass来降低和增加线程的优先级.

I don't understand how to use SetThreadPriority and SetPriorityClass to lower and increase the priority of a Thread.

我的理解是SetPriorityClass选择进程可用的优先级范围,而SetThreadPriority设置类中的相对优先级.

My understanding is that the SetPriorityClass selects the range of priorities available to a process and the SetThreadPriority sets the relative priority within the class.

例如,对线程执行此操作的结果是什么?

For instance, what is the result of doing this for a thread :

SetPriorityClass(GetCurrentProcess(), PROCESS_MODE_BACKGROUND_BEGIN);

SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);

感谢您的帮助.

推荐答案

进程优先级类和线程优先级正在构建线程的base priority.请参见计划优先级查找优先级如何组合.通过查看此列表,可以清楚地了解您的理解是正确的.在特定优先级类别中,base priority可以具有由thread priority确定的各种值.

The process priority class and the thread priority are building the base priority of a thread. See Scheduling Priorities to find how the priorities are assembled. By looking at this list it becomes clear that your understanding is somewhat correct; within a certain priority class the base priority can have various values, determined by the thread priority.

并非所有Windows版本都支持SetPriorityClassPROCESS_MODE_BACKGROUND_BEGIN值和SetThreadPriorityTHREAD_MODE_BACKGROUND_END值.

The PROCESS_MODE_BACKGROUND_BEGIN value for SetPriorityClass and the THREAD_MODE_BACKGROUND_END value for SetThreadPriority are not supported on all Windows versions.

PROCESS_MODE_BACKGROUND_BEGIN:系统降低了进程(及其线程)的资源调度优先级,因此它可以执行后台工作,而不会显着影响前台的活动.

THREAD_MODE_BACKGROUND_END:结束后台处理模式.系统将恢复线程的资源调度优先级,就像线程进入后台处理模式之前一样.

这里所讨论的场景的结果是可以预见的:SetPriorityClass会将其所有线程的进程设置为background processing mode.以下SetThreadPriority将仅释放background processing mode中的线程.但是该进程的所有其他可能线程将保持处于后台处理模式.

The consequence of the scenario in question here is predictable: The SetPriorityClass will set the process with all of its threads into background processing mode. The following SetThreadPriority will only release the a thread from background processing mode. But all other possible threads of the process will stay in in background processing mode.

注意:只有process priority class thread priority的组合才能确定base priority.因此,对GetThreadPriority的调用或对GetPriorityClass的调用都不会返回基本优先级.只有它们的组合才能释放基本优先级,这在上面的调度优先级"链接中进行了描述.不幸的是,新的background processing mode值尚未包含在base priority列表中.但是名称base priority在这里说明了重要的事情:基于基本优先级(从进程优先级类和线程优先级派生),允许调度程序动态调整调度优先级.后台模式只是fine tune调度优先级的另一种方式.另一种方法是优先级提升.优先级提升功能存在一段时间.对SetThreadPrioritySetPriorityClassbackground processing mode值的新访问直接打开了优先级提升功能.在Windows XP中,必须通过调用 SetProcessPriorityBoost .

Note: Only the combination of process priority class and thread priority determines the base priority. Therefore neither a call to GetThreadPriority nor a call to GetPriorityClass will return the base priority. Only their combination releases the base priority which is described in the "Scheduling Priorities" link above. Unfortunately the new background processing mode values aren't yet included in the base priority list. But the name base priority tells what matters here: Based on the base priority (derived from process priority class and thread priority) the scheduler is allowed to dynamically adapt the scheduling priority. The background mode is just another way to fine tune the scheduling priority. Another way are Priority Boosts. The priority boost functionality exists for some time. The new access to background processing mode values for SetThreadPriority and SetPriorityClass opens the priority boost capability directly. In Windows XP this had to be done by a call to SetProcessPriorityBoost.

这篇关于SetThreadPriority和SetPriorityClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 16:12