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

Time:05-23

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?

CodePudding user response:

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.

  • Related