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();
}
}