进程优先级和线程池优先级(C#)之间的任何关系 [英] Any relationship between process priority and thread pool priority (C#)

查看:605
本文介绍了进程优先级和线程池优先级(C#)之间的任何关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解正在运行的进程应该/不能更改线程池的优先级,但是线程池上运行的特定任务的优先级是否与调用进程的优先级相提并论?

I understand that thread pool priority should/can not be changed by the running process, but is the priority of particular task running on the thread pool somewhat priced-in with the calling process priority?

换句话说,线程池中的所有任务是否都以相同的优先级运行,而不管调用进程的优先级如何?

in other words, do all tasks in thread pool run in the same priority regardless the calling process priority?

谢谢

更新1:我应该更具体一些,我指的是Parallel.ForEach内部的线程

update 1: i should have been more specific, i refer to thread inside Parallel.ForEach

推荐答案

我了解正在运行的进程应该/不能更改线程池优先级,

I understand that thread pool priority should/can not be changed by the running process,

那是不正确的.您可以更改线程池的线程优先级(在委托本身内部),它将以新的优先级运行,但是默认值将在其任务完成时恢复,并将其发送回池中.

That's not exact. You can change Thread Pool's thread priority (inside delegate itself) and it'll run with new priority but default one will be restored when its task finishes and it'll be send back to pool.

ThreadPool.QueueUserWorkItem(delegate(object state) {
    Thread.CurrentThread.Priority = ThreadPriority.Highest;

    // Code in this function will run with Highest priority
});

线程池上运行的特定任务的优先级是否与调用进程的优先级相比有所定价?

is the priority of particular task running on the thread pool somewhat priced-in with the calling process priority?

是的,它不仅适用于线程池的线程.在Windows中,进程的优先级由其类指定(从IDLE_PRIORITY_CLASSREALTIME_PRIORITY_CLASS).连同线程的优先级(从THREAD_PRIORITY_IDLETHREAD_PRIORITY_TIME_CRITICAL),它将用于计算线程的 final 优先级.

Yes, and it doesn't apply to Thread Pool's threads only. In Windows process' priority is given by its class (from IDLE_PRIORITY_CLASS to REALTIME_PRIORITY_CLASS). Together with thread's priority (from THREAD_PRIORITY_IDLE to THREAD_PRIORITY_TIME_CRITICAL) it'll be used to calculate thread's final priority.

从MSDN:

将进程优先级类别和线程优先级组合在一起,以形成每个线程的基本优先级.

The process priority class and thread priority level are combined to form the base priority of each thread.

请注意,它不仅是基本优先级加上偏移量:

Note that it's not simply a base priority plus an offset:

NORMAL_PRIORITY_CLASS + THREAD_PRIORITY_IDLE  == 1
NORMAL_PRIORITY_CLASS + THREAD_PRIORITY_TIME_CRITICAL == 15

但是:

REALTIME_PRIORITY_CLASS + THREAD_PRIORITY_IDLE == 16
REALTIME_PRIORITY_CLASS + THREAD_PRIORITY_TIME_CRITICAL == 31

此外,线程可以具有临时的 boost (由Windows Scheduler决定和管理).请注意,进程还可以更改其自己的优先级类别.

Moreover threads can have a temporary boost (decided and managed by Windows Scheduler). Be aware that a process can also change its own priority class.

换句话说,线程池中的所有任务是否都以相同的优先级运行,而不管调用进程的优先级如何?

in other words, do all tasks in thread pool run in the same priority regardless the calling process priority?

否,线程的优先级取决于进程的优先级(请参阅上一段),并且池中的每个线程可以 temporary 具有不同的优先级.还要注意,线程优先级不受调用者线程优先级的影响:

No, thread's priority depends on process' priority (see previous paragraph) and each thread in pool can temporary have a different priority. Also note that thread priority isn't affected by caller thread's priority:

ThreadPool.QueueUserWorkItem(delegate(object s1) {
    Thread.CurrentThread.Priority = ThreadPriority.Highest;

    ThreadPool.QueueUserWorkItem(delegate(object s2) {
        // This code is executed with ThreadPriority.Normal

        Thread.CurrentThread.Priority = ThreadPriority.Lowest;

        // This code is executed with ThreadPriority.Lowest
    });

    // This code is executed with ThreadPriority.Highest
});

编辑 :. NET任务使用线程池,而不是上面写的内容.例如,如果要枚举Parallel.ForEach来增加线程优先级,则必须在循环内完成

EDIT: .NET tasks uses Thread Pool than what wrote above still applies. If, for example, you're enumerating a collecition with Parallel.ForEach to increase thread priority you have to do it inside your loop:

Parallel.ForEach(items, item => {
    Thread.CurrentThread.Priority = ThreadPriority.Highest;

    // Your code here...
});

只是警告:更改优先级时要小心.例如,如果两个线程使用共享资源(受锁保护),则有许多争夺该资源的竞争,而其中一个具有最高优先级,那么您可能会以很高的CPU使用率结束(因为

Just a warning: be careful when you change priorities. If, for example, two threads use a shared resource (protected by a lock), there are many races to acquire that resources and one of them has highest priority then you may end with a very high CPU usage (because of spinning behavior of Monitor.Enter). This is just one issue, please refer to MSDN for more details (increasing thread's priority may even result is worse performance).

这篇关于进程优先级和线程池优先级(C#)之间的任何关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆