在 Windows 中更改 boost 线程优先级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/601337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change boost thread priority in Windows
提问by Lodle
Im trying to change the thread priority in boost but im having no luck. Im getting a bad handle error (type 6) from the GetLastError function. I though native_handle() returned the handle for the thread?
我试图在 boost 中更改线程优先级,但我没有运气。我从 GetLastError 函数得到一个错误的句柄错误(类型 6)。我虽然 native_handle() 返回了线程的句柄?
Any one know how to do this?
有人知道怎么做吗?
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: Final 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
}
采纳答案by Dani van der Meer
Use SetThreadPriority function to set the thread priority. SetPriorityClass is used to set the priority of the process. You also have to change the priority values, see documentation for SetThreadPriorityfor details.
使用 SetThreadPriority 函数设置线程优先级。SetPriorityClass 用于设置进程的优先级。您还必须更改优先级值,有关详细信息,请参阅SetThreadPriority 的文档。
回答by 1800 INFORMATION
The SetPriorityClass
function takes as it's first parameter a HANDLE, you are passing in a pointer to a HANDLE. Change it to:
该SetPriorityClass
函数将第一个参数作为 HANDLE 参数,您传递的是一个指向 HANDLE 的指针。将其更改为:
res = SetPriorityClass(*th, REALTIME_PRIORITY_CLASS);
or something equivalent. The kernel can tell that the pointer value you passed in isn't really a valid thread handle because I guess it maintains an internal list of currently allocated thread handles. The pointer obviously isn't in that list. The compiler can't really enforce better type safety, since a HANDLE is kind of an opaque type - you just have to be really careful what you pass in.
或等价的东西。内核可以判断您传入的指针值并不是真正有效的线程句柄,因为我猜它维护了当前分配的线程句柄的内部列表。指针显然不在该列表中。编译器不能真正强制执行更好的类型安全,因为 HANDLE 是一种不透明的类型——你只需要非常小心你传入的内容。
Oh by the way, the other commenter Dani is correct, SetPriorityClass
is not used for setting the priority of a thread, you want to use SetThreadPriority
anyway. But then my advice would still stand, you need to pass in a HANDLE, not a pointer to such.
哦对了,另一个评论者Dani是对的,SetPriorityClass
不是用来设置线程的优先级的,SetThreadPriority
反正你想用。但是我的建议仍然有效,你需要传入一个 HANDLE,而不是一个指向它的指针。