Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change boost thread priority in Windows

Tags:

Im trying to change the thread priority in boost but im having no luck. Im getting a bad handle error (type 6) from the GetLastError function. I though native_handle() returned the handle for the thread?

Any one know how to do this?

void baseThread::applyPriority(uint8 priority) {  #ifdef WIN32     if (!m_pThread)         return;      BOOL res;     HANDLE th = m_pThread->native_handle();      switch (priority)     {     case REALTIME   : res = SetPriorityClass(th, REALTIME_PRIORITY_CLASS);      break;     case HIGH       : res = SetPriorityClass(th, HIGH_PRIORITY_CLASS);          break;     case ABOVE_NORMAL   : res = SetPriorityClass(th, ABOVE_NORMAL_PRIORITY_CLASS);  break;     case NORMAL     : res = SetPriorityClass(th, NORMAL_PRIORITY_CLASS);            break;     case BELOW_NORMAL   : res = SetPriorityClass(th, BELOW_NORMAL_PRIORITY_CLASS);  break;     case IDLE       : res = SetPriorityClass(th, IDLE_PRIORITY_CLASS);          break;     }      if (res == FALSE)     {         int err = GetLastError();     }  #endif } 

edit: Final code:

void baseThread::applyPriority(uint8 priority) {  #ifdef WIN32     if (!m_pThread)         return;      BOOL res;     HANDLE th = m_pThread->native_handle();      switch (priority)     {     case REALTIME       : res = SetThreadPriority(th, THREAD_PRIORITY_TIME_CRITICAL);   break;     case HIGH           : res = SetThreadPriority(th, THREAD_PRIORITY_HIGHEST);         break;     case ABOVE_NORMAL   : res = SetThreadPriority(th, THREAD_PRIORITY_ABOVE_NORMAL);    break;     case NORMAL         : res = SetThreadPriority(th, THREAD_PRIORITY_NORMAL);          break;     case BELOW_NORMAL   : res = SetThreadPriority(th, THREAD_PRIORITY_BELOW_NORMAL);    break;     case IDLE           : res = SetThreadPriority(th, THREAD_PRIORITY_LOWEST);          break;     }  #endif } 
like image 417
Lodle Avatar asked Mar 02 '09 05:03

Lodle


People also ask

How do I change the priority of a thread?

The thread priority determines when the processor is provided to the thread as well as other resources. It can be changed using the method setPriority() of class Thread. There are three static variables for thread priority in Java i.e. MIN_PRIORITY, MAX_PRIORITY and NORM_PRIORITY.

What is Windows dynamic thread priority boost?

The Dynamic Thread Priority Boost provides a brief additional priority boost when a thread is awoken from a wait state. So, for instance, when an app becomes active, it gets a tiny short-lived boost in priority.

What is the difference between base and dynamic priority?

Initially, a thread's dynamic priority is the same as its base priority. The system can boost and lower the dynamic priority, to ensure that it is responsive and that no threads are starved for processor time. The system does not boost the priority of threads with a base priority level between 16 and 31.

What is thread priority in C++?

Threads provided by the operating system usually have priorities. There is currently no way of setting the priority on a thread with the current C++ thread APIs. However, by using std::thread::native_handle , one can get a handle to the underlying operating system thread and use native APIs for setting priorities.


1 Answers

Use SetThreadPriority function to set the thread priority. SetPriorityClass is used to set the priority of the process. You also have to change the priority values, see documentation for SetThreadPriority for details.

like image 86
Dani van der Meer Avatar answered Oct 02 '22 20:10

Dani van der Meer