简体   繁体   English

有没有办法使用boost线程库设置线程关联到处理器核心?

[英]Is there a way to set thread affinity to a processor core with the boost thread library?

And would it be a good idea to do so if I needed the processor cores running on 100% for a dozen seconds or would I get better performance if I let the system decide how to handle the threads? 如果我需要处理器内核100%运行十几秒,或者如果我让系统决定如何处理线程,我会获得更好的性能,这样做是否是一个好主意?

What I need is fast execution and I'm worried the system might spend a few seconds before using all the cores, but I haven't found any way of doing this with boost thread. 我需要的是快速执行,我担心系统可能会在使用所有内核之前花费几秒钟,但我还没有找到任何使用boost线程的方法。

Your operating system's scheduler should take care of this for you on the order of milliseconds, not seconds. 您的操作系统的调度程序应该按照毫秒而不是秒的顺序为您处理。 The details depend on which OS you're running on, but one common approach is that when the scheduler runs on an idle CPU, it will check to see if it can steal a thread from another CPU that has too many. 详细信息取决于您正在运行的操作系统,但一种常见方法是,当调度程序在空闲CPU上运行时,它将检查是否可以从另一个具有太多CPU的CPU中窃取一个线程。 That quickly balances the threads across all CPUs. 这可以快速平衡所有CPU的线程。

As a general rule, if you need to set CPU affinity, either you have a very specific and unusual use case, or your operating system has a bug. 作为一般规则,如果您需要设置CPU亲和性,您要么具有非常特定且不寻常的用例,要么您的操作系统存在错误。

In very high performance systems, performance improves if you assign low memory footprint tasks that are sensitive to cache to a particular core and the core does pretty much only this task. 在性能非常高的系统中,如果将对缓存敏感的低内存占用任务分配给特定内核,并且内核几乎只执行此任务,则性能会提高。 The effect is to improve cache hit rates which can make a huge difference. 效果是提高缓存命中率,这可以产生巨大的差异。 If this is not the case, the leave things to the OS's scheduler. 如果不是这种情况,请将操作保留给操作系统的调度程序。 It is (as noted above) a rather specialised situation, but when they arise, processor affinity is worth looking into. 它(如上所述)是一种相当专业的情况,但当它们出现时,处理器亲和力值得研究。

You'll first need to call the get_native_handle member function then pass the obtained handle to a platform specific function to set the thread's CPU affinity (ie pthread_setaffinity_np/SetThreadAffinityMask/...). 首先需要调用get_native_handle成员函数,然后将获取的句柄传递给特定于平台的函数,以设置线程的CPU亲和性(即pthread_setaffinity_np / SetThreadAffinityMask / ...)。

As to if that's a good idea: profile & find out... 至于这是一个好主意:个人资料并找出...

There is one more reason for affinity set. 亲和力设定还有另外一个原因。 For some modern systems (eg ARM based SOCs) power saving technology significantly affects scheduler. 对于一些现代系统(例如基于ARM的SOC),节电技术显着影响调度器。 The system run as minimum cpu cores as possible. 系统尽可能以最小cpu核心运行。 If all current tasks can live on one core only the other cores are sleeping. 如果所有当前任务都可以存在于一个核心上,则只有其他核心正在休眠。 If the load suddenly rises (running new heavy task) there is one or more cores need to be waked up. 如果负载突然上升(运行新的繁重任务),则需要唤醒一个或多个核心。 This process is relatively slow, so it can give your task going lack of cpu while scheduler is balancing load for more cores. 这个过程相对较慢,因此它可以使您的任务缺乏cpu,而调度程序正在平衡更多内核的负载。 With manual affinity set to a few threads/processes you can keep all cores running (and always be ready for sudden load rise). 通过将手动关联设置为几个线程/进程,您可以保持所有核心运行(并始终为突然的负载上升做好准备)。 It looks like to be good for pseudo real-time tasks. 它似乎对伪实时任务有好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM