2009-03-02 10 views
23

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

Qualcuno sa come fare questo?

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 
} 

risposta

18

funzione Usa 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 i dettagli.

+0

quello era un errore stupido. Mi piacerebbe provarlo ora. Grazie – Lodle

3

La funzione SetPriorityClass prende come primo parametro una MANIGLIA, si passa un puntatore a una MANIGLIA. Cambiarlo a:

res = SetPriorityClass(*th, REALTIME_PRIORITY_CLASS); 

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

Oh a proposito, l'altro commentatore Dani è corretto, SetPriorityClass non viene utilizzato per impostare la priorità di un thread, si desidera utilizzare comunque SetThreadPriority. Ma poi il mio consiglio rimarrà valido, è necessario passare in una MANIGLIA, non un puntatore a tale.

+0

La cosa del puntatore era che mi occupavo del codice in anticipo e non lo aggiustavo. Ma grazie per il tuo aiuto in ogni modo. : P – Lodle

Problemi correlati