在 C++11 中设置 std::thread 优先级的便携式方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18884510/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:18:12  来源:igfitidea点击:

Portable way of setting std::thread priority in C++11

c++c++11portabilitystdthreadthread-priority

提问by Gerdiner

What is the correct way in the post C++11 world for setting the priority of an instance of std::thread

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

Is there a portable way of doing this that works at least in Windows and POSIX (Linux) environments?

是否有至少在 Windows 和 POSIX (Linux) 环境中有效的便携式方法?

Or is it a matter of getting a handle and using whatever native calls are available for the particular OS?

或者是获取句柄并使用可用于特定操作系统的任何本机调用?

回答by Mike Seymour

There's no way to set thread priorities via the C++11 library. I don't think this is going to change in C++14, and my crystal ball is too hazy to comment on versions after that.

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

In POSIX, pthread_setschedparam(thread.native_handle(), policy, {priority});

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

I don't know the equivalent Windows function, but I'm sure there must be one.

我不知道等效的 Windows 功能,但我确定一定有一个。

回答by marc

My quick implementation...

我的快速实施...

#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;
};

and this is how I use it...

这就是我如何使用它...

// create thread
std::thread example_thread(example_function);

// set scheduling of created thread
thread::setScheduling(example_thread, SCHED_RR, 2);

回答by Dietmar Kühl

The standard C++ library doesn't define any access to thread priorities. To set thread attributes you'd use the std::thread's native_handle()and use it, e.g., on a POSIX system with pthread_getschedparam()or pthread_setschedparam(). I don't know if there are any proposals to add scheduling attributes to the thread interface.

标准 C++ 库没有定义对线程优先级的任何访问。要设置线程属性,您将使用std::thread'snative_handle()并使用它,例如,在 POSIX 系统上使用pthread_getschedparam()pthread_setschedparam()。不知道有没有给线程接口添加调度属性的建议。

回答by Rodrigo Rutsatz

In Windows processes are organized in class and level priority. Read this: Scheduling Priorities, it gives a good overall knowledge about thread and process priority. You can use the following functions to control the priorities even dynamically: GetPriorityClass(), SetPriorityClass(), SetThreadPriority(), GetThreadPriority().

在 Windows 中,进程按类和级别优先级进行组织。阅读:调度优先级,它提供了有关线程和进程优先级的全面知识。您甚至可以使用以下函数来动态控制优先级:GetPriorityClass()SetPriorityClass()SetThreadPriority()GetThreadPriority()

Apperantly you can also use std::thread's native_handle()with pthread_getschedparam()or pthread_setschedparam()on a windows system. Check this example, std::thread: Native Handleand pay attention to the headers added!

Apperantly你也可以使用std::threadnative_handle()pthread_getschedparam()pthread_setschedparam()在Windows系统上。检查这个例子,std::thread: Native Handle并注意添加的标题!