2 ms·
We've experimented a lot with thread affinity and our conclusion is that more often than not playing with thread affinity brings no performance advantage and is
by shin_lao 11y ago
We've experimented a lot with thread affinity and our conclusion is that more often than not playing with thread affinity brings no performance advantage and is very problematic to work with on different platforms.
For example on Linux the name of the mask set is cpu_set_t and on BSD it is cpuset_t. On OS X you have to use the function thread_policy_set and on Windows SetThreadAffinityMask, all with different logics.
The other problem is once you play with affinity you have to take care of all the threads of your application because if you leave one thread roaming on all cores your affinity approach is ruined.
Making sure that different steps of the same operation are processed in the same logical thread makes a bigger performance difference than playing with thread affinity.
Last but not least, the code in this article is incorrect. You must first do sched_getaffinity to know the cores on which your program is allowed to run and then do pthread_setaffinity_np.
- pjmlp 11y agoIt really made a difference back in Windows NT 3.51 and 4.0. We used it to keep a Apache threads per core on our servers back in those days. The scheduler wasn't still optimized for the .com loads and the Apache threads kept jumping between cores. Also Apache still wasn't that optimized for Windows as well.