Frank Liu
Frank Liu

Reputation: 1606

C++ Multithreading on Multicore system

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

Upvotes: 2

Views: 5938

Answers (3)

Non-maskable Interrupt
Non-maskable Interrupt

Reputation: 3911

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.

Upvotes: 11

Jakub
Jakub

Reputation: 573

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...

Upvotes: 1

Nokdu
Nokdu

Reputation: 98

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

Upvotes: 0

Related Questions