LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 0
Setting thread affinity


Hi,

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?

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

Rep: Reputation: 179Reputation: 179
Quote:
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.
 
  


Reply



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 09:31 AM
cpu affinity process hlds multiple CosmicDebris Linux - Games 2 01-09-2008 01:55 PM
CPU affinity on kernel 2.4.14-SMP tsik Linux - Kernel 0 11-05-2007 09:27 PM
set_thread_area failed when setting up thread-local storage Dr Thrall Linux - Newbie 2 12-18-2005 02:39 PM
Processor affinity and cpu scheduling...? Thaidog Linux - Newbie 5 01-09-2003 07:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 PM.

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