1
votes

This is a somewhat generic question. Suppose that I am creating a simple application in Java or VC++ which creates two threads and I run the application in a multicore system. Without any specific directive for which core to run, will the application itself will distribute the threads across various cores?

Thanks.

3

3 Answers

8
votes

The application will not distribute the threads, but the OS will. That's its job. :)

It's interesting to note that threads can swap between cores quite frequently in the course of a single running process due to other loads and interrupts that can hit a cpu core.

If you decide that you need specify the core that a particular thread can run on, the OS will generally provide a mechanism set the thread's "affinity" to a specific cpu. This may be valuable in advanced optimization scenarios to keep a particular thread's cache hot, e.g. It's generally not needed, though.

3
votes

The OS assigns processes and threads to different cores. That is not to say you cannot influence the decision, some OSs allow you some control on deciding this. Processes may also move between cores as necessary to ensure good utilisation of cores.

1
votes

You can do this on Windows with SetThreadIdealProcessor and associated API calls:

http://msdn.microsoft.com/en-us/library/ms684251(VS.85).aspx

If your threads are each demanding a lot of resources then you would expect the OS to distribute them across the available cores.

I'd be a little wary about messing with the thread affinity as effectively you're second guessing the OS's scheduler. For example you might cause your application to become less responsive but scheduling threads in such a way as to compete with your application's primary UI thread.

I typically approach things in the following order with the help of a profiler and some fixed application benchmarks so I can compare different approaches

  1. The algorithm
  2. The algorithm - Seriously! Before thinking about anything else worry about the algorithm.
  3. Compiler switches - Do you have all optimisations enabled for your release build.
  4. Compiler hints - Make sure your code given the compiler as much information as possible so it can optimise. For example are you overusing pointers or some other mechanism which the compiler can't second guess.
  5. Caching, thread affinity etc.

Ade