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?
thank you
update 1: i should have been more specific, i refer to thread inside Parallel.ForEach
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.
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
toREALTIME_PRIORITY_CLASS
). Together with thread's priority (fromTHREAD_PRIORITY_IDLE
toTHREAD_PRIORITY_TIME_CRITICAL
) it'll be used to calculate thread's final priority.From MSDN:
Note that it's not simply a base priority plus an offset:
But:
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.
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:
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: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).