7
votes

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

3
The same way that multiple processes run: timesharing. - Barmar
On most non embedded devices your kernel sets up an interrupt to fire after a certain time, then loads one of your threads and runs in userspace, then when the interrupt fires, the interrupt handler goes back into kernel space, the kernel then sometimes switches to another thread, sets up the interrupt again. Rinse and repeat. - PeterT
I have an i7 with 4/8 cores. It is managing 1215 threads. No problem, because hardly any of them are running. - Martin James
you mean it has 4 cores right ? - Suraj Jain

3 Answers

10
votes

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.

9
votes

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

0
votes

The processor core is capable of executing one thread at a time. In a quad core, 4 threads are executed simultaneously. Not all the user space threads are executed simultaneously, the kernel threads also runs to schedule the next thread or do some other kernel tasks.