Ändern Sie die Boost-Thread-Priorität in Windows

Ändern Sie die Boost-Thread-Priorität in Windows


Ich versuche, die Thread-Priorität in Boost zu ändern, aber ich habe kein Glück. Ich erhalte einen fehlerhaften Handle-Fehler (Typ 6) von der GetLastError-Funktion. Ich habe zwar native_handle() das Handle für den Thread zurückgegeben?


Weiß jemand wie das geht?


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
}

Bearbeiten:Endgültiger 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
}

Antworten:


Verwenden Sie die SetThreadPriority-Funktion, um die Thread-Priorität festzulegen. SetPriorityClass wird verwendet, um die Priorität des Prozesses festzulegen. Sie müssen auch die Prioritätswerte ändern, siehe Dokumentation für SetThreadPriority für Details.