April 2020 personal diary

2020/4/18

Concurrency and parallelism are different.

On a single CPU core, threads switch tasks through time slices or give up control to achieve the purpose of running multiple tasks "simultaneously". This is called concurrency. But in fact, only one task is executed at any time, and the other tasks are queued through an algorithm.

Multi-core CPUs can allow "multiple threads" in the same process to run simultaneously in the true sense, which is parallel.

Process, thread, coroutine

Process: A process is the basic unit of resource allocation by the system and has an independent memory space.

Threads: Threads are the basic unit of CPU scheduling and dispatch. Threads depend on the existence of processes, and each thread shares the resources of the parent process.

Coroutine: Coroutine is a lightweight thread in user mode. The scheduling of coroutine is completely controlled by the user. Switching between coroutines only needs to save the context of the task without the overhead of the kernel.

Guess you like

Origin www.cnblogs.com/wangby511/p/12729754.html