Alterar prioridade impulso fio no Windows
-
03-07-2019 - |
Pergunta
Im tentando mudar a prioridade de thread no impulso, mas estou tendo nenhuma sorte. Estou ficando um mau erro alça (tipo 6) a partir da função GetLastError. I embora native_handle () retornou a alça para o segmento?
qualquer um sabe como fazer isso?
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: código final:
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
}
Solução
Use a função SetThreadPriority para definir a prioridade de thread. SetPriorityClass é usado para definir a prioridade do processo. Você também tem que alterar os valores de prioridade, consulte a documentação para SetThreadPriority para obter mais informações.
Outras dicas
A função SetPriorityClass
toma como seu primeiro parâmetro uma alça, você está passando um ponteiro para uma alça. Alterá-lo para:
res = SetPriorityClass(*th, REALTIME_PRIORITY_CLASS);
ou algo equivalente. O kernel pode dizer que o valor do ponteiro que você passou em não é realmente um identificador segmento válido porque eu acho que mantém uma lista interna de identificadores de thread actualmente afectados. O ponteiro, obviamente, não está nessa lista. O compilador não pode realmente fazer cumprir melhor segurança de tipos, uma vez que um identificador é uma espécie de tipo opaco -. Você só tem que ser muito cuidadoso com o que se passa em
Ah, a propósito, outro comentarista Dani está correto, SetPriorityClass
não é usado para definir a prioridade de um segmento, você quer usar SetThreadPriority
de qualquer maneira. Mas, então, o meu conselho ainda estaria, você precisa passar em uma alça, não um ponteiro para tal.