1

I don't know how to thread in C++ and I would not just wan't to know that but is there a way i can force a thread onto a different core? Also how would I find out how many cores the user has?

  • 1
    You cannot force a thread to run in *parallel* in standard C++. That is at the environment's underlying thread scheduler (usually in the kernel). All you are guaranteed is *concurrency*. – obataku Sep 03 '12 at 04:05
  • 2
    ... but, anyways, you should try using the [new `thread` library](http://en.cppreference.com/w/cpp/thread) in C++11. – obataku Sep 03 '12 at 04:11
  • These are really three questions. The most specific one, about choosing the core, has been [asked before](http://stackoverflow.com/questions/4886886/assigning-a-thread-to-specific-cpu-core). – jogojapan Sep 03 '12 at 04:16

1 Answers1

4

Binding thread to the arbitrary CPU is called setting affinity. It's platform-dependent operation.

For windows: SetProcessAffinityMask

For pthreads: pthread_attr_setaffinity_np(3) and pthread_setaffinity_np(3)

For Boost you can use native_handle() to get platform-specific thread handle to use them with functions above.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • 1
    ... right; C++11 equivalent would be to access the similar [`thread::native_handle`](http://en.cppreference.com/w/cpp/thread/thread/native_handle). Though it should be warned that this is non-standard. – obataku Sep 03 '12 at 04:14
  • Note that the `native_handle` function is standard. What you *get* from it is what is "non-standard". – Nicol Bolas Sep 15 '12 at 07:23