Fix Threads to execute on certain cores [duplicate]
Possible Duplicate:
Bind threads to processors
In Linux, is it possible to fix threads to execute on certain cores. If that is possible, I also want to know if it is possible to execute one th开发者_StackOverflowread exclusively on a certain core, that is disallowing any other thread to execute on that core while that thread is executing.
That's what pthread_setaffinity_np(3)
does.
The
pthread_setaffinity_np()
function sets the CPU affinity mask of the thread thread to the CPU set pointed to by cpuset. If the call is successful, and the thread is not currently running on one of the CPUs in cpuset, then it is migrated to one of those CPUs.
As an example:
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(3, &set); /* Run only on the third CPU. */
pthread_setaffinity_np(thr, CPU_SETSIZE, &set);
You can also do it with sched_setaffinity(2)
and gettid
, but that manual page says:
If you are using the POSIX threads API, then use
pthread_setaffinity_np(3)
instead ofsched_setaffinity()
.
精彩评论