Search code examples
multithreadingparallel-processinglinux-kernelschedulerscheduling

When 2 threads would be executed on a 1 physical CPU core with a multi-core CPU machine?


Lets say there's a machine with 8-cores CPU.

I'm creating 2 posix threads using standard pthread_create(...) function.

As I know there's no any garanties these threads always would be executed by a 2 different physical cores, but practically in 90% they will run simultaneously (or in parallel). At least for my cases I seen that top command shows 2 cpu's are running ... thus around 160-180% CPU usage

The question is:

What could be the scenario when 2 threads within a single process are running only on 1 physical core ?


Solution

  • Two cases:

    1) The other physical cores are busy doing other stuff, so only one core gets used by this process. The two threads run in alternation on that core.

    2) The physical core supports executing more than one thread concurrently using hyperthreading or something similar. The other physical cores are busy doing other stuff, so the best the scheduler can do is run both threads in a single physical core.