2

When running the following code on a four core CPU, am I guaranteed these four threads are going to be run on four difference cores?

Or the OS could put multiple threads on a single core depending on the workload of the system. If this is the case, running multiple-thread may even give you performance penalties due to context-switching and other threading overhead.

I suppose there is no way to code the application, which forces the threads to be run on different cores?

int main()
{
    std::vector<std::thread> threads(3);

    for (size_t i = 0; i < 3; i++)
    {
        threads[i] = std::thread(DoSomethingLengthy);
    }

    DoSomethingLengthy();

    for (auto& thread : threads)
    { 
        thread.join(); 
    }
}
Frank Liu
  • 1,466
  • 3
  • 23
  • 36
  • There are two questions here... – KiaMorot Sep 12 '14 at 08:48
  • 1
    First read about [processor affinity](http://en.wikipedia.org/wiki/Processor_affinity). And to answer your question: Unless you "pin" a thread to a specific core, it's up to the operating system which core that runs your thread. The operating system could even change the core multiple times during the time a thread is active. – Some programmer dude Sep 12 '14 at 08:50
  • 1
    You get no such guarantees, you program needs to compete with whatever other programs are running on the machine. This is *not* a problem, and especially doesn't need any help, operating systems invariable know how to do this well. – Hans Passant Sep 12 '14 at 08:53
  • @JoachimPileborg understand the OS can change cores to run a thread. As long as it schedule four threads on four different cores, it is fine by me. The question I am really asking is that is it possible for the OS to schedule two or more threads on the same core during the lifetime of the software. – Frank Liu Sep 12 '14 at 08:54
  • Yes, it's possible for multiple threads in the same process to end up on the same core, it's even possible for *all* threads of a single process to be on the same core. In fact, this may even be preferred, if the threads share data, as the threads then will share the CPU caches. – Some programmer dude Sep 12 '14 at 08:55

3 Answers3

9

Do not play with affinity mask unless you know what you are doing.

On modern system, the scheduling is a complex logic that involve thermo throttling, cache locality, NUMA distance and many factors. The system know far better for the current machine state than you (or otherwise you don't even have access to any information) to make better decision.

For instant, the scheduler may decide to put multiple threads on same core (running at 4GHz) just to let the other core to cool down(running at 500MHz), and the result is still better than evenly putting threads on all cores.

Non-maskable Interrupt
  • 3,841
  • 1
  • 19
  • 26
1

Threads are under control of the operating system. The second answer is correct. There are OS specific APIs to control this, (which typically require higher privileges,) but the c++ answer is you are not guaranteed...

Jakub
  • 583
  • 3
  • 8
0

you could try setting the affinity mask

the link is windows only.. so if you are working on a different OS you could check this comment too

Community
  • 1
  • 1
Nokdu
  • 98
  • 7