Domanda

Sto cercando di cambiare la priorità del thread in boost ma non ho fortuna. Sto ottenendo un errore di handle errato (tipo 6) dalla funzione GetLastError. Anche se native_handle () ha restituito l'handle per il thread?

Qualcuno sa come farlo?

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
}

modifica: codice finale:

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
}
È stato utile?

Soluzione

Utilizzare la funzione SetThreadPriority per impostare la priorità del thread. SetPriorityClass viene utilizzato per impostare la priorità del processo. È inoltre necessario modificare i valori di priorità, consultare la documentazione per SetThreadPriority per dettagli.

Altri suggerimenti

La funzione SetPriorityClass accetta come primo parametro un HANDLE, si passa un puntatore a un HANDLE. Modificalo in:

res = SetPriorityClass(*th, REALTIME_PRIORITY_CLASS);

o qualcosa di equivalente. Il kernel può dire che il valore del puntatore che hai passato non è in realtà un handle di thread valido perché immagino che mantenga un elenco interno di handle di thread attualmente allocati. Il puntatore ovviamente non è in quella lista. Il compilatore non può davvero applicare una migliore sicurezza dei tipi, dal momento che un HANDLE è un tipo di tipo opaco - devi solo stare molto attento a ciò che passi.

Oh, a proposito, l'altro commentatore Dani ha ragione, SetPriorityClass non è usato per impostare la priorità di un thread, vuoi comunque usare SetThreadPriority . Ma poi il mio consiglio sarebbe ancora valido, è necessario passare una MANIGLIA, non un puntatore a tale.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top