我正在尝试更改 boost 中的线程优先级,但我没有运气。我从 GetLastError 函数中收到错误的句柄错误(类型 6)。我虽然 native_handle() 返回了线程的句柄?
有人知道怎么做吗?
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
}
编辑:最终代码:
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
}
最佳答案
使用 SetThreadPriority 函数设置线程优先级。 SetPriorityClass 用于设置进程的优先级。您还必须更改优先级值,参见 SetThreadPriority 的文档。了解详情。
关于c++ - 在 Windows 中更改 boost 线程优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/601337/