Is there any relation between CPU and threads? [cl

2019-08-30 08:09发布

问题:

If there is, what is the relation?

回答1:

Think of a CPU as a person, and a Thread as a single task that person has to perform.

For instance, if one person tries to wash the dishes, prepare breakfast, put clothes on the children, and make sure the cat doesn't eat the dog food, that person has to switch back and forth between tasks rapidly, in order to have some progress on all tasks seemingly at the same time.

But that person can only do one thing at a time.

Revisit 2013: Note that the above is not necessarily true. First of all, a single "processor" now has multiple cores, each capable of doing multiple things at the same time with pipelines and what not. It's a far more complex world than just "a single CPU can do only one thing at a time".



回答2:

A single CPU (core) can run only one thread at once.

If you have more threads than CPUs, they will be scheduled using some sort of time slicing algorithm.



回答3:

A single thread can be run by a CPU at a time, that is why when you make a multi-threaded application the concept of dual-core or multi-core can be utilized to paraller process your program units.



回答4:

A thread is a unit of work assigned to a CPU core.

At any given moment any thread is either suspended (not running) or is running on exactly one core. Every core is either idle (not running any thread) or is running exactly one thread.

Assigning threads to cores is the operating system scheduler job.



回答5:

There are multiple relation possible, depending on your system architecture:

  • for single processor case, the relation is 1:N (where N is the number of threads). Everything will be run on that single processor
  • for multi processor/multi core/hyperthreading case, the relation is approximately P:N (where P is the number of processors), however, the thread exectution becomes a 'multi-dimensional' scheduling problem.

Your operating system's thread scheduler will make effort to distribute the threads among the available processing units, based on runtime characteristics (used cpu cycles, blockedness), user preferences (affinity, thread(s) serving the active window) and other properties.

For example, if you have 1 intensive computational thread and 10 I/O thread with lots of waits, the OS scheduler will most likely assign T1 to P1 and T2-T11 to P2 and power down the rest of the cores for energy saving and cache-redistribution to favor P1. T1 will become the entire core for its use and does not get preempted by T2-T11.