views:

2200

answers:

2

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
}
+7  A: 

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.

Dani van der Meer
that was a stupid error. Ill give it a go now. Thanks
Lodle
+1  A: 

The SetPriorityClass function takes as it's first parameter a HANDLE, you are passing in a pointer to a HANDLE. Change it to:

res = SetPriorityClass(*th, REALTIME_PRIORITY_CLASS);

or something equivalent. The kernel can tell that the pointer value you passed in isn't really a valid thread handle because I guess it maintains an internal list of currently allocated thread handles. The pointer obviously isn't in that list. The compiler can't really enforce better type safety, since a HANDLE is kind of an opaque type - you just have to be really careful what you pass in.

Oh by the way, the other commenter Dani is correct, SetPriorityClass is not used for setting the priority of a thread, you want to use SetThreadPriority anyway. But then my advice would still stand, you need to pass in a HANDLE, not a pointer to such.

1800 INFORMATION
The pointer thing was me messing with the code before hand and not fixing it up. But thanks for your help any way. :P
Lodle