C++ 在C+中设置std::线程优先级的可移植方法+;11

C++ 在C+中设置std::线程优先级的可移植方法+;11,c++,c++11,portability,stdthread,thread-priority,C++,C++11,Portability,Stdthread,Thread Priority,在后C++11时代,设置std::thread实例优先级的正确方法是什么 是否有一种至少在Windows和POSIX(Linux)环境下工作的可移植方法 或者是获取一个句柄并使用特定的OS调用什么本地调用? 标准C++库没有定义对线程优先级的任何访问。要设置线程属性,您需要使用std::thread的native\u handle()并使用它,例如,在带有pthread\u getschedparam()或pthread\u setschedparam()的POSIX系统上。我不知道是否有任何建

在后C++11时代,设置std::thread实例优先级的正确方法是什么

是否有一种至少在Windows和POSIX(Linux)环境下工作的可移植方法


或者是获取一个句柄并使用特定的OS调用什么本地调用?

标准C++库没有定义对线程优先级的任何访问。要设置线程属性,您需要使用
std::thread
native\u handle()
并使用它,例如,在带有
pthread\u getschedparam()
pthread\u setschedparam()的POSIX系统上。我不知道是否有任何建议将调度属性添加到线程接口。

无法通过C++11库设置线程优先级。我认为这在C++14中是不会改变的,我的水晶球太模糊了,无法评论之后的版本

在POSIX中,
pthread_setschedparam(thread.native_handle(),policy,{priority})


我不知道等效的Windows函数,但我确信一定有一个。

在Windows中,进程是按类和级别优先级组织的。阅读以下内容:,它提供了关于线程和进程优先级的全面知识。您甚至可以使用以下函数动态控制优先级:、


显然,您也可以在windows系统上使用
std::thread
native\u handle()
pthread\u getschedparam()
pthread\u setschedparam()
。检查此示例,并注意添加的标题

我的快速实现

#include <thread>
#include <pthread.h>
#include <iostream>
#include <cstring>

class thread : public std::thread
{
  public:
    thread() {}
    static void setScheduling(std::thread &th, int policy, int priority) {
        sch_params.sched_priority = priority;
        if(pthread_setschedparam(th.native_handle(), policy, &sch_params)) {
            std::cerr << "Failed to set Thread scheduling : " << std::strerror(errno) << std::endl;
        }
    }
  private:
    sched_param sch_params;
};

您可以使用以下代码在Windows中设置优先级

#if defined(_WIN32)
    /* List of possible priority classes:
    https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass
    And respective thread priority numbers:
    https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
    */
    DWORD dwPriorityClass = 0;
    int nPriorityNumber = 0;
    tasks::getWinPriorityParameters(setPriority, dwPriorityClass, nPriorityNumber);
    int result = SetPriorityClass(
            reinterpret_cast<HANDLE>(mainThread.native_handle()),
            dwPriorityClass);
    if(result != 0) {
          std::cerr << "Setting priority class failed with " << GetLastError() << std::endl;
    }
    result = SetThreadPriority(
            reinterpret_cast<HANDLE>(mainThread.native_handle()),
            nPriorityNumber);
    if(result != 0) {
          std::cerr << "Setting priority number failed with " << GetLastError() << std::endl;
    }
#endif
#如果已定义(_WIN32)
/*可能的优先级别列表:
https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass
和相应的线程优先级编号:
https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
*/
DWORD dwPriorityClass=0;
int nPriorityNumber=0;
任务::getWinPriorityParameters(setPriority、dwPriorityClass、nPriorityNumber);
int结果=SetPriorityClass(
重新解释\u转换(mainThread.native\u handle()),
dwPriorityClass);
如果(结果!=0){

std::cerr减1表示不想知道它在Windows中是如何实现的。加1表示知道它无法通过C++11进行移植。加1表示不想知道它在Windows上是如何实现的。实际上@MarkusMayr这在显示真正的实现时非常有用。其他答案只是引用了函数,但从未显示适当的示例e、 它可能不是惯用的,但我相信它演示了为特定线程设置优先级的概念。至少,它帮助了我。但是,请注意,此代码通过调用
native_handle()依赖于特定于实现的行为
;标准不要求该函数存在,如果存在,则标准不要求该函数具有任何特定含义。除其名称外,它的所有内容均由实现定义。另一个完整示例:此问题(和答案)仍然是相关的,并且可能永远是。没有特定于语言的方法来实现这一点的原因是线程的实现(如果实现了线程)线程的调度是操作系统实现的一个细节。作为一种语言,C++没有定义平台。另一方面,java定义了一个提供某些资源(例如线程)的虚拟机。。通过询问POSIX(一种标准化的操作系统界面),您已经接近您的答案。现代Windows也实现了POSIX(理论上,我从未使用过它),因此按照POSIX规范编码应该可以让您尽可能接近可移植性。下面的POSIX答案是很好的。这个答案比其他答案更准确。
#if defined(_WIN32)
    /* List of possible priority classes:
    https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass
    And respective thread priority numbers:
    https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
    */
    DWORD dwPriorityClass = 0;
    int nPriorityNumber = 0;
    tasks::getWinPriorityParameters(setPriority, dwPriorityClass, nPriorityNumber);
    int result = SetPriorityClass(
            reinterpret_cast<HANDLE>(mainThread.native_handle()),
            dwPriorityClass);
    if(result != 0) {
          std::cerr << "Setting priority class failed with " << GetLastError() << std::endl;
    }
    result = SetThreadPriority(
            reinterpret_cast<HANDLE>(mainThread.native_handle()),
            nPriorityNumber);
    if(result != 0) {
          std::cerr << "Setting priority number failed with " << GetLastError() << std::endl;
    }
#endif