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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 16:49:39

问题


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?

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.


回答1:


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 (i.e. pthread_setaffinity_np/SetThreadAffinityMask/...).

As to if that's a good idea: profile & find out...




回答2:


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. That quickly balances the threads across all CPUs.

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.




回答3:


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.




回答4:


There is one more reason for affinity set. For some modern systems (e.g. ARM based SOCs) power saving technology significantly affects scheduler. The system run as minimum cpu cores as possible. 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. 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.



来源:https://stackoverflow.com/questions/13335652/is-there-a-way-to-set-thread-affinity-to-a-processor-core-with-the-boost-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!