简体   繁体   中英

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?

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.

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.

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/...).

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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