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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With