gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:31:47 27 4
gpt4 key购买 nike

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

我正在使用 Raspbian Stretch

这是针对主线程的:

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

我收到Invalid argument 并且我不明白为什么...

祝福

最佳答案

如果策略无法识别/不受支持或参数没有意义,则返回 EINVAL。 SCHED_OTHER 仅对优先级为 0 的线程有意义。您遇到的行为是类似 FreeBSD 的系统的典型行为,它们不会忽略 sched_pa​​ram::priority 非零值。

关于c++ - 在 C++11 中为 pthread 设置调度参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52696772/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com