Wednesday, February 4, 2009

Bind a thread/process to a CPU core

#include <sched.h>
  • Bind the invoke thread to a CPU core which id is p
void thread_bind(int p)
{
cpu_set_t mask;
__CPU_ZERO(&mask);
__CPU_SET(p, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
}
  • Bind thread which id is tid to a CPU core p
void thread_bind(int p, int tid)
{
cpu_set_t mask;
__CPU_ZERO(&mask);
__CPU_SET(p, &mask);
sched_setaffinity(tid, sizeof(mask), &mask);
}

Reference
  1. Get thread id

No comments:

Post a Comment