Introduction to Concurrency and Parallelism

content

1. Processes and threads

2. Concurrency and Parallelism

3. Coroutines and threads

Fourth, concurrency is not parallel


1. Processes and threads

1) A process is an execution process of a program in the operating system, an independent unit for the system to allocate and schedule resources. 2) A thread is an execution entity of a process, and it is the basic unit of CPU scheduling and dispatching. It is a basic unit smaller than a process that can run independently. 3) A process can create and revoke multiple threads; multiple threads in the same process can execute concurrently.

2. Concurrency and Parallelism

A. A multi-threaded program running on a core cpu is concurrency. B. A multi-threaded program running on multiple cores of cpu is parallel.

Concurrency

parallel

 

3. Coroutines and threads

Coroutine: independent stack space, shared heap space, scheduling is controlled by the user, which is essentially similar to user-level threads, and the scheduling of these user-level threads is also implemented by itself.

Thread: A thread can run multiple coroutines, and coroutines are lightweight threads.

Goroutine is only the super "thread pool" implemented by the official. The stack memory footprint of each instance of 4~5KB and the greatly reduced creation and destruction overhead due to the implementation mechanism are the fundamental reasons for the high concurrency of go.

Fourth, concurrency is not parallel

Concurrency is mainly achieved by switching time slices to achieve "simultaneous" operation, while parallelism is to directly use multi-core to achieve multi-threaded operation. Go can set the number of cores used to exert the capabilities of multi-core computers. Instead of sharing memory, goroutines communicate to share memory.

The concept of goroutines is similar to threads, but goroutines are scheduled and managed by the Go program runtime. Go programs intelligently distribute tasks in goroutines to each CPU reasonably. The Go program starts from the main() function of the main package. When the program starts, the Go program creates a default goroutine for the main() function.

 

Guess you like

Origin blog.csdn.net/demored/article/details/124094204