You are here:

C++/process and thread priority


QUESTION: Dear Zlatko,
In my application, there are two threads. I want to set one of them the highest priority, and the other one second higest.I mean, they are not gonging to be disturbed by any application running in my computer. How can I realize?
I have tryed SetPriorityClass and SetThreadPriority, these functions work correctly, but, the result is not what I deduce.
Thank you very much.

ANSWER: Dear Xiyuan

In my experience, SetPriorityClass and SetThreadPriority are the only two functions you need to change a threadís priority.  You need to be an administrator to achieve REALTIME priority, but a process without admin rights can set itself to be HIGH priority.  I suppose that you have checked the return values of the priority setting functions to ensure they succeeded.

My understanding is that the priority class and thread priority are starting priorities. Threads may get priority boosts which may interfere with your high priority threads. This could happen when the window of a normal priority process is brought to the foreground or when the conditions of a blocked thread are satisfied. The page
talks about priority boosts.

Your threads may be experiencing priority inversion if they are waiting on resources held by lower priority processes.

You should expect that other threads will be given time to run on your system. Otherwise your system will become unresponsive. It is important that HIGH_PRIORITY or REALTIME_PRIORITY threads issue sleeps or waits so that other threads can run.

Perhaps you could include more details about what you are observing on your system and how it differs from what you expect. If you are using some small test program, post it so that I may have a look at it.

Best regards.

---------- FOLLOW-UP ----------

QUESTION: Dear Zlatko,
Thanks for answering my question.As you guess, I have checked the return values of the priority setting functions to ensure they succeeded.But now, I'm not sure whether the parameters I used in SetPriorityClass and SetThreadPriority right or wrong.
The first parameter in SetPriorityClass should be a handle to process whose priority is being setting.Here, I used GetCurrentProcess to get the handle.As you know, the return value of this function is pseudo handle. Should I translate it to a real handle for using in SetPriorityClass? Or, can I use m_hInstance in class CWinApp(My class derives from class CWinApp)?
The same question come up to me when I use function SetThreadPriority.
What's your idea?
Thank you.

ANSWER: Dear Xiyuan

According to the documentation, the pseudo handle is interpreted as the current process for functions that need a process handle. The pseudo handle has the most permissive access to the process. You would need a real handle only if you wanted the handle to be inherited by child processes. Similarly, GetCurrentThread can only be used by the current thread to refer to itself.

I used SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) and noticed in the task
manager that the priority was changed.

Using CWinApp::m_hInstance certainly does not work. The HINSTANCE is always 0x00400000 for an executable. I've been told that it represents the virtual memory load address of the process.

It sounds like you are doing everything right by using

I hope that helps you.
Best regards

---------- FOLLOW-UP ----------

QUESTION: Dear Zlatko
After many tests, it seemed that process priority class did not have influence on thread priority.
After I changed thread to a certain degree, no matter which priority class I changed process to, the value of thread priority did not change. For example, if I set thread priority to THREAD_PRIORITY_TIME_CRITICAL, the value of thread priority was always 15, no matter the priority class of process was(REALTIME_PRIORITY_CLASS, or NORMAL_PRIORITY_CLASS or others).
Where should the problem be?
Thank you.

Xiyuan, I am just responding to this follow up to remove it from my pending list.
As I said in the previous follow-up, I think the thread priority level is some sort of offset from the process priority class, and the actual priority of the thread is a combination of the two numbers but I cannot find a function to return that combination.


Best regards.


All Answers

Answers by Expert:

Ask Experts




No longer taking questions.


No longer taking questions.

No longer taking questions.

©2016 All rights reserved.