在c++11中为pthread设置调度参数

在c++11中为pthread设置调度参数,c++,c++11,pthreads,raspbian,C++,C++11,Pthreads,Raspbian,我真的想给我的程序的主线程一个更高的调度优先级。我用c++11线程尝试了这个,但失败了 我用的是拉斯宾拉伸法 这是针对主线程的: sched_param sch; int policy; pthread_getschedparam(pthread_self(), &policy, &sch); sch.sched_priority = 10; if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sch)) {

我真的想给我的程序的主线程一个更高的调度优先级。我用c++11线程尝试了这个,但失败了

我用的是拉斯宾拉伸法

这是针对主线程的:

sched_param sch;
int policy;
pthread_getschedparam(pthread_self(), &policy, &sch);
sch.sched_priority = 10;
if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sch)) {
    std::cout << "Failed to setschedparam: " << std::strerror(errno) << std::endl;
}
对于c++11线程,这是:

std::unique_ptr<std::thread> Stream;
Stream = std::make_unique<std::thread>([&, this]
{
    while (!finished)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(2));
        /* more code */
    }
});
sched_param sch;
int policy;
pthread_getschedparam(Stream->native_handle(), &policy, &sch);
sch.sched_priority = 20;
if (pthread_setschedparam(Stream->native_handle(), SCHED_OTHER, &sch))
{
    std::cout << "Failed to setschedparam: " << std::strerror(errno) << std::endl;
}
我得到了无效的论点,我不明白为什么


如果策略无法识别/不受支持或参数不合理,则会返回最美好的祝愿。SCHED_OTHER仅适用于优先级为0的线程。你所遇到的行为是FreeBSD类系统中的一个典型的行为,他们不忽略SeeDePARAM::优先级非零值。

当你的问题特别是关于C++时,请停止标记无关的语言。我认为C是相关的,因为调用函数与C++无关。但我想问题是关于c++的,好的,它与0一起工作。例如,如果希望优先级为10,则需要将第二个参数设置为什么?@i7clock取决于您想要的排序和计时类型。SHED_RR或SHED_FIFO是常用的选择,您必须阅读手册。