c++ - 在 Windows 中更改 boost 线程优先级

标签 c++ windows multithreading winapi boost

我正在尝试更改 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/

相关文章:

c++ - 如何获取 Mac OS X Dock 的位置、宽度和高度? cocoa /碳/C++/Qt

c++ - QtCreator 中的 "Fatal Error C1083: Cannot open include file"

Windows 资源管理器外壳扩展 : create file and enter "rename" mode

c# - 试图用类名填充文本框

java - 这是在 Java 中错误使用内部条件队列吗?

c++ - 是否可以在 C++ 中自动生成析构函数?

c++ - C++ 中的 set<pair> 和 map 有什么区别?

windows - Windows 10 的 "task scheduler"存储日志文件在哪里?

java - 如何立即强制终止 ThreadPoolExecutor 中的所有 worker

java - 我得到了以下代码片段的竞争条件