简体   繁体   English

在Windows中更改提升线程优先级

[英]Change boost thread priority in Windows

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. 我从GetLastError函数得到一个错误的句柄错误(类型6)。 I though native_handle() returned the handle for the thread? 我虽然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
}

Use SetThreadPriority function to set the thread priority. 使用SetThreadPriority函数设置线程优先级。 SetPriorityClass is used to set the priority of the process. SetPriorityClass用于设置进程的优先级。 You also have to change the priority values, see documentation for SetThreadPriority for details. 您还必须更改优先级值,有关详细信息,请参阅SetThreadPriority的文档。

The SetPriorityClass function takes as it's first parameter a HANDLE, you are passing in a pointer to a HANDLE. SetPriorityClass函数将第一个参数作为HANDLE,你传入一个指向HANDLE的指针。 Change it to: 将其更改为:

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. 哦顺便说一句,其他评论者Dani是正确的, SetPriorityClass不用于设置线程的优先级,你想要使用SetThreadPriority But then my advice would still stand, you need to pass in a HANDLE, not a pointer to such. 但是我的建议仍然存在,你需要传递一个HANDLE,而不是指向这样的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM