Search code examples
multithreadingcpu-usagecpu-cores

Relation of the words :: 'Thread' and 'Core'


Does the number of threads created/able by a program has any relation/constrain with the number or core that CPU has? Does the word thread has any relation with Cores of CPU at all?


Solution

  • A computer can only run number_of_cores threads in parallel. If a system has only a single core, only one thread runs at once. Multitasking is only "simulated" by running each thread consecutively for very short periods of time.

    Normally the OS schedules threads arbitrarily on cores. For instance a thread's one quantum could be run on one core and the same thread's next quantum could be run on another core. That's why you see both cores busy when you run a single cpu-heavy thread. This allows single-threaded processes take advantage of multicore systems by distributing load among multiple cores, depending on varying availability.

    More than that, operating systems allow a thread to be "locked" on one or more cores, so it runs dedicated on specified cores only. That's called affinity mask on Windows. Today's multicore aware software can take advantage of that and dedicate certain threads to certain cores to optimize its workload. Games do that for dedicating AI for one core, rendering for another for instance. In order to do that efficiently the software has to know how many cores a system has and create and assign threads based on that number.

    Similarly parallel programming frameworks such as OpenMP count the cores to parallelize operations efficiently. They create as many threads as the number of cores on the system to get optimal performance for physical parallelism.

    I think that's the only case of a relation between number of cores and number of threads.