Pthread CPU Affinity

This is a discussion on Pthread CPU Affinity within the C Programming forums, part of the General Programming Boards category; Is there any way to run a POSIX thread with a predetermined cpu affinity? I am using pthread_setaffinity_np right now ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    93

    Pthread CPU Affinity

    Is there any way to run a POSIX thread with a predetermined cpu affinity? I am using pthread_setaffinity_np right now but the problem with that approach is that it only works with a thread that is already created. pthread_setaffinity_np simply moves the thread to another cpu if the current cpu that it is running on does not match the cpu mask passed in. If my pogram is constantly creating worker threads this seems like unnecessary overhead. You have no control over what cpu the thread actually starts on. Is there any way to set the cpu on which a POSIX thread will start as a result of calling pthread_create?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    If you are creating threads often enough, consider using a thread-pool concept, where once a thread is finished with a task, it goes "back in the pool" and waits until it needs to process another task. So you would have a master thread (or API) for giving out tasks to threads in the pool. This avoids thread creation/destruction overhead.

    The other option is to use pthread_attr_setaffinity_np() on an attribute object, which you use to create the threads.

    gg

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    Yeah I am using the thread pool concept and it is working well. I tried using the pthread_setaffinity_np on a pthread_attr_t object but that didnt appear to work. When i called pthread_setaffinity_np on this struct before the thread was created using pthread_create and it returned EINVAL...

    from man page:

    EINVAL (pthread_setaffinity_np()) The affinity bit mask mask contains no
    processors that are currently physically on the system and permitted to
    the thread according to any restrictions that may be imposed by the
    "cpuset" mechanism described in cpuset(7).

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    I called pthread_setaffinity_np on the thread_t struct.. not the attribute so what I described above is incorrect. Can you pass pthread_attr_t to pthread_setaffinity_np?

  5. #5

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    Well that's embarrassing.... somehow i completely overlooked that method in all of the reading I did

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  2. Upgrading my old CPU (for another old one!)
    By foxman in forum Tech Board
    Replies: 16
    Last Post: 01-11-2008, 04:41 PM
  3. Can you still view the bios screen with a bad CPU?
    By HyperCreep in forum Tech Board
    Replies: 4
    Last Post: 12-31-2006, 05:57 PM
  4. pthread and multiple CPU
    By noelloen in forum C Programming
    Replies: 4
    Last Post: 03-07-2006, 11:00 AM
  5. CPU temp
    By PING in forum Tech Board
    Replies: 5
    Last Post: 01-28-2006, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21