Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does more than one thread execute on a processor core

I wanted to know how does a multi-threaded program with more number of threads executes on a processor core. For example, my program has 12 threads and I am running it on a intel core-i5 machine. It has four CPUs. Will each core run 3 threads? I am confused because I have seen programs with 30 threads running on a 4 core machine.

Thanks

like image 389
user225008 Avatar asked Nov 01 '14 00:11

user225008


People also ask

Can multiple threads run on one core?

In computer architecture, multithreading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system. This approach differs from multiprocessing.

How do cores have multiple threads?

Instead of giving a large workload to a single core, threaded programs split the work into multiple software threads. These threads are processed in parallel by different CPU cores to save time. Depending on how they're built, games may be lightly threaded or heavily threaded.

Can a core have more than 2 threads?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads.

How do multiple threads work?

With multithreading, while the computer system's processor executes one instruction at a time, different threads from multiple programs are executed so fast it appears the programs are executed simultaneously.


2 Answers

A core is responsible for executing thread cycles. The more cores you have, the more threads you may run simultaneously. Each core can execute only one instruction at a time, however it’s so fast it seems as if you are running multiple threads simultaneously. Intel Processors supports Hyper Threading enabling a single core to support multiple Threads as operating system sees twice the amount of logical cores per physical core. For instance, a Core i3, which is only a dual core, can actually serve two threads per core i.e. total of four threads can run simultaneously. However even if Core i5 processors are quad cores, since they don’t support Hyper-Threading (except the i5-661) the number of threads they can serve at the same time is just about equal to those of their Core i3 counterparts.

like image 92
Piyush Mattoo Avatar answered Oct 10 '22 01:10

Piyush Mattoo


Each core would be able to execute one thread simultaneously. So if there are 30 threads and 4 cores, 26 threads will be waiting to get context switched to get executed. Something like, thread 1-4 runs for 200ms and then 5-8 runs for 200 ms and so on

like image 33
Steve Avatar answered Oct 09 '22 23:10

Steve