Threads & Processes Vs MultiThreading & Multi-Core/MultiProcessor : How they are mapped?

后端 未结 7 1999
灰色年华
灰色年华 2020-12-07 08:49

I was very confused but the following thread cleared my doubts:

Multiprocessing, Multithreading,HyperThreading, Multi-core

But it addresses the queries from

7条回答
  •  感动是毒
    2020-12-07 08:59

    Threads running on the same core are not technically parallel. They only appear to be executed in parallel, as the CPU switches between them very fast (for us, humans). This switch is what is called context switch. Now, threads executing on different cores are executed in parallel. Most modern CPUs have a number of cores, however, most modern OSes (windows, linux and friends) usually execute much larger number of threads, which still causes context switches. Even if no user program is executed, still OS itself performs context switches for maintanance work.
    This should answer 1-3.

    About 4: basically, every processor can work with threads. it is much more a characteristic of operating system. Thread is basically: memory (optional), stack and registers, once those are replaced you are in another thread.

    5: the number of threads is pretty high and is limited by OS. Usually it is higher than regular programmer can successfully handle :) The number of threads is dictated by your program:

    is it IO bound?

    • can the task be divided into a number of smaller tasks?
    • how small is the task? the task can be too small to make it worth to spawn threads at all.
    • synchronization: if extensive synhronization is required, the penalty might be too heavy and you should reduce the number of threads.

提交回复
热议问题