C++ 使用 Boost 在 Linux 中设置线程优先级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1479945/
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
Setting thread priority in Linux with Boost
提问by SlickMcRunFast
The Boost Libraries don't seem to have a device for setting a thread's priority. Would this be the best code to use on Linux or is there a better method?
Boost 库似乎没有用于设置线程优先级的设备。这是在 Linux 上使用的最佳代码还是有更好的方法?
boost::thread myThread( MyFunction() );
struct sched_param param;
param.sched_priority = 90;
pthread_attr_setschedparam( myThread.native_handle(), SCHED_RR, ¶m);
I don't have alot of Linux programming experience.
我没有很多 Linux 编程经验。
采纳答案by Duck
That's the basic template for how I would do it but after searching around I found next to no code examples so I guess the verdict is out on whether it is best or not.
这是我将如何做的基本模板,但在四处搜索之后我发现几乎没有代码示例,所以我想判断它是否最好。
The problem is that boost::thread does not have a constructor that allows pthead attributes to be passed in at thread creation so you have to make changes after the thread starts. The only other way I know to get around that is through the process/thread schedule inheritance. Unless directed otherwise, new threads will inherit the schedule/priority of their creator so you could change the current thread before creating worker threads and then change back if you want. Seems awkward but it is an alternative.
问题是 boost::thread 没有允许在线程创建时传入 pthead 属性的构造函数,因此您必须在线程启动后进行更改。我知道的唯一另一种解决方法是通过进程/线程调度继承。除非另有指示,否则新线程将继承其创建者的调度/优先级,因此您可以在创建工作线程之前更改当前线程,然后根据需要更改回来。看起来很尴尬,但它是另一种选择。
Here's a hack of an example that hopefully demostrates both. You may need to change policy and priority as appropriate and run as root.
这是一个示例的 hack,希望能同时演示两者。您可能需要根据需要更改策略和优先级并以 root 身份运行。
Be careful with the way you set the priority. Various restrictions apply.
请注意设置优先级的方式。适用各种限制。
#include <iostream>
#include <boost/thread/thread.hpp>
#include <unistd.h>
#include <sched.h>
#include <cstdio>
void* threadfunc()
{
sleep(5);
}
void displayAndChange(boost::thread& daThread)
{
int retcode;
int policy;
pthread_t threadID = (pthread_t) daThread.native_handle();
struct sched_param param;
if ((retcode = pthread_getschedparam(threadID, &policy, ¶m)) != 0)
{
errno = retcode;
perror("pthread_getschedparam");
exit(EXIT_FAILURE);
}
std::cout << "INHERITED: ";
std::cout << "policy=" << ((policy == SCHED_FIFO) ? "SCHED_FIFO" :
(policy == SCHED_RR) ? "SCHED_RR" :
(policy == SCHED_OTHER) ? "SCHED_OTHER" :
"???")
<< ", priority=" << param.sched_priority << std::endl;
policy = SCHED_FIFO;
param.sched_priority = 4;
if ((retcode = pthread_setschedparam(threadID, policy, ¶m)) != 0)
{
errno = retcode;
perror("pthread_setschedparam");
exit(EXIT_FAILURE);
}
std::cout << " CHANGED: ";
std::cout << "policy=" << ((policy == SCHED_FIFO) ? "SCHED_FIFO" :
(policy == SCHED_RR) ? "SCHED_RR" :
(policy == SCHED_OTHER) ? "SCHED_OTHER" :
"???")
<< ", priority=" << param.sched_priority << std::endl;
}
int main(int argc, char* argv[])
{
int policy, res;
struct sched_param param;
if ((policy = sched_getscheduler(getpid())) == -1)
{
perror("sched_getscheduler");
exit(EXIT_FAILURE);
}
if ((res = sched_getparam(getpid(), ¶m)) == -1)
{
perror("sched_getparam");
exit(EXIT_FAILURE);
}
std::cout << " ORIGINAL: ";
std::cout << "policy=" << ((policy == SCHED_FIFO) ? "SCHED_FIFO" :
(policy == SCHED_RR) ? "SCHED_RR" :
(policy == SCHED_OTHER) ? "SCHED_OTHER" :
"???")
<< ", priority=" << param.sched_priority << std::endl;
policy = SCHED_RR;
param.sched_priority = 2;
if ((res = sched_setscheduler(getpid(), policy, ¶m)) == -1)
{
perror("sched_setscheduler");
exit(EXIT_FAILURE);
}
boost::thread t1(&threadfunc);
displayAndChange(t1);
t1.join();
return 0;
}
回答by knulp
You can have a look at this library (written by a student on mine, not released yet, look at the svn repository): https://sourceforge.net/projects/threadutility/
你可以看看这个库(由我的一个学生写的,尚未发布,查看 svn 存储库):https: //sourceforge.net/projects/threadutility/
Basically, he wrote a wrapper of the boost::thread that allows to specify and set threads' priorities in a portable way, by selecting the right internal implementation depending on the platform (currently Linux or Windows). In Linux, the code uses the sched_setscheduler() syscall.
基本上,他编写了一个 boost::thread 的包装器,通过根据平台(当前为 Linux 或 Windows)选择正确的内部实现,允许以可移植的方式指定和设置线程的优先级。在 Linux 中,代码使用 sched_setscheduler() 系统调用。
回答by tiberious726
boost::thread
does have the ability to take in pthread attributes before pthread_create()
is called. It provides the type boost::thread::attributes
, which itself can only be used to set the stack size (on systems that support it), but the attributes object also presents a .get_native_handle()
method, which returns a pthread_attr_t
, on which you can set your desired attributes. You can then simply call make_thread()
with that boost::thread::attributes
object as an argument. See the second and third code boxes in this section: http://www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html#thread.thread_management.tutorial.attributes
boost::thread
确实有能力在pthread_create()
调用之前接受 pthread 属性。它提供了 type boost::thread::attributes
,它本身只能用于设置堆栈大小(在支持它的系统上),但 attributes 对象还提供了一个.get_native_handle()
方法,它返回 a pthread_attr_t
,您可以在其上设置所需的属性。然后,您可以简单地make_thread()
将该boost::thread::attributes
对象作为参数进行调用。请参阅本节中的第二个和第三个代码框:http: //www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html#thread.thread_management.tutorial.attributes
回答by Arafangion
I don't think Linux really has thread priorities - in most kernels you can use 'Nice' levels, but that is probably about it (Which would simplify the scheduler) - however, not all linux systems will respect that! (Depends on the scheduler).
我不认为 Linux 真的有线程优先级——在大多数内核中你可以使用“Nice”级别,但这可能就是这样(这将简化调度程序)——但是,并非所有的 Linux 系统都会尊重这一点!(取决于调度程序)。