LinuxAnswers - the LQ Linux tutorial section.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-16-2009, 09:20 AM   #1
LQ Newbie
Registered: Jan 2009
Posts: 18

Rep: Reputation: 0
Setting thread affinity


I've noticed the latest pthread library includes the following API:
int pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset);
int pthread_attr_getaffinity_np(pthread_attr_t *attr, size_t cpusetsize, cpu_set_t *cpuset);

Is there a way to change these attributes "on-the-fly", meaning, after the thread is created?

Old 02-17-2009, 02:56 AM   #2
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
Is there a way to change these attributes "on-the-fly", meaning, after the thread is created?
I've never used this feature, but I'm going to stick my neck out and say "no".

The various functions in the POSIX threads interface are named in a carefully consistent way. The feature in which you're interested is reflected in the API with that same careful consistency.

If there had been any way specifically to change the CPU affinity on the fly, one would expect <pthread.h> to name additional functions whose names include "affinity". But there are none.

Or if there were a way to apply thread attributes to an already-existing thread, one would expect <pthread.h> to name an additional function to do so, and its name would end in "_np", since all non-portable functions in <pthread.h> have names that end thus. But I've looked at each non-portable function declaration in that file, and none of them do this.

But you don't need that capability. If you have n different combinations of affinities you want a given thread to switch between, just define n threads, with all of them but one waiting on its own condition variable which (when signaled) indicates that this thread has the currently desired affinity. When it comes time to switch affinities, the thread which is currently running should signal the appropriate condition for the desired thread, and then itself wait on its own condition, to be signaled if it's ever the case in the future that its own affinity is appropriate.

Additional care must be taken to allow such waiting threads to be allowed to continue, but go straight to exit instead of doing normal processing; otherwise you'd never be able to join those threads later, if you want to do so.

If you're using pthread_setspecific() , then allocate a block of data to be shared by these threads, and let each thread's specific data point to that shared data. You won't need to use mutual exclusion on any of that data, because only these threads (which are for identical work, except that the affinities differ) will be using that data, and only one of these threads will be working at any given time.

Hope this helps.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you set processor affinity on Linux? Thaidog Linux - General 1 02-27-2008 10:31 AM
cpu affinity process hlds multiple CosmicDebris Linux - Games 2 01-09-2008 02:55 PM
CPU affinity on kernel 2.4.14-SMP tsik Linux - Kernel 0 11-05-2007 10:27 PM
set_thread_area failed when setting up thread-local storage Dr Thrall Linux - Newbie 2 12-18-2005 03:39 PM
Processor affinity and cpu scheduling...? Thaidog Linux - Newbie 5 01-09-2003 08:08 PM

All times are GMT -5. The time now is 12:35 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration