linuxmultithreadingtaskset

linux taskset: Does a thread of a multi-thread process always run on a particular core?


I use the taskset to set a multi-thread process to run on a Linux host as below:

task -c 1,2 ./myprocess

Will a particular thread always run on a particular CPU, for example, thread 1 always run on c1? or it will run on c1 or c2 at different times?


Solution

  • No, the filter is applied to the whole process and threads can move between (the restricted list of) cores. If you want threads not to move, then you need set the affinity of each thread separately (eg. using pthread_setaffinity_np for example). Note that you can check the affinity of threads of a given process with the great hwloc tool (hwloc-ps -t).

    Note that some libraries/frameworks have ways to do that more easily. This is the case for OpenMP programs where you can use environment variables like OMP_PLACES to set the affinity of each thread.