开发者

Fix Threads to execute on certain cores [duplicate]

开发者 https://www.devze.com 2023-03-24 12:29 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Bind threads to processors
This question already has answers here: Closed 11 years ago.

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 of sched_setaffinity().

0

精彩评论

暂无评论...
验证码 换一张
取 消