views:

1581

answers:

1

Hi,

I'd like to specify the cpu-affinity of a particular pthread. All the references I've found so far deal with setting the cpu-affinity of a process (pid_t) not a thread (pthread_t). I tried some experiments passing pthread_t's around and as expected they fail. Am I trying to do something impossible? If not, can you send a pointer please? Thanks a million.

+1  A: 

Assuming linux:

The interface to setting the affinity is - as you've probably already discovered:

int sched_setaffinity(pid_t pid,size_t cpusetsize,cpu_set_t *mask);

Passing 0 as the pid, and it'll apply to the current thread only, or have other thread report their kernel pid with the linux specific call pid_t gettid(void); and pass that in as the pid.

Quoting the man page

The affinity mask is actually a per-thread attribute that can be adjusted independently for each of the threads in a thread group. The value returned from a call to gettid(2) can be passed in the argument pid. Specifying pid as 0 will set the attribute for the calling thread, and passing the value returned from a call to getpid(2) will set the attribute for the main thread of the thread group. (If you are using the POSIX threads API, then use pthread_setaffinity_np (3) instead of sched_setaffinity().)

nos