Go, developed by Google's Robert Griesemer, Rob Pike and Ken Thompson in 2009, is a statically typed, garbage collection, multi-threaded concurrency programming language. The Go language design goal is to be simple, efficient and easy to use. Its concurrency model is very powerful and can easily handle a large number of concurrent tasks, making Go an ideal choice for modern high-performance concurrent applications.
In Go (Golang),concurrent(Concurrency) andparallel(Parallelism) is two concepts that are both related and different
Concurrency
Concurrency refers to the ability to start, execute and complete over a period of time that appears to be performed simultaneously at the macro level, although at the micro level they may not be really performed simultaneously. Go supports efficient concurrent programming through goroutines (lightweight threads) and channels (secure channels for communication between goroutines). The focus of concurrency is on the organization and management of tasks, so that even on a single CPU core, users can be given the illusion that multiple tasks are performed simultaneously through time slicing and task scheduling.
Parallelism
Parallel means that multiple processors (or multi-core CPUs) perform multiple tasks simultaneously on physical or logically. When a program can make full use of multiple cores of a multi-core processor, such that different tasks or parts of tasks are indeed executed on different processors at the same time, this is parallelism. The Go language runtime can automatically utilize multi-core processors, so that when enough goroutines need to be run, they can be allocated to execute in parallel on different CPU cores, thereby improving program execution efficiency.
The implementation principle of goroutine
The implementation principle of goroutine mainly depends on the runtime and scheduler of Go language. When we create a goroutine, the runtime allocates a separate stack space to it and adds it to the scheduler's run queue. The scheduler will schedule their execution based on the priority and state of the goroutine (run, sleep, block, etc.).
The implementation principle of channel
The implementation principle of channel mainly depends on the runtime and memory model of Go language. When we create a channel, the runtime allocates a buffer to store the data. The read and write operations of the channel are atomic, which means they are not interrupted. When a goroutine writes data to the channel, the runtime will put the data into the buffer and notify other goroutines. When a goroutine reads data from the channel, the runtime will fetch the data from the buffer and notify other goroutines.
Relationships and differences
- the difference: Concurrently, the task execution method and collaboration between tasks can be carried out in a single-core or multi-core environment, and does not guarantee that the task will be executed at the same time. Parallelism focuses on the actual simultaneous execution of tasks, which requires a multi-core environment to implement.
- connect: Concurrency is the basis of parallelism, and there is no parallelism without concurrency. Concurrency enables the program to be logically decomposed into multiple independently executed units, while parallelism implements the simultaneous execution of these units at the hardware level, thereby achieving higher performance.
for i := 1; i <= 10; i++ { go func() { ("123") }() }
This code shows the concurrency characteristics in Go language.for
Loop and passgo
The keyword-started goroutines are executed concurrently. The specific analysis is as follows:
Concurrency: when
for
When the loop is each iteration, it starts a new goroutine and passesgo func() {...}()
expression. This means that when the loop continues on the next iteration, the newly launched goroutine starts execution almost immediately without waiting for the previous goroutine to complete. Therefore, the execution of these goroutines is concurrent - their execution overlaps in time, but the actual execution order depends on the scheduling policy of the Go runtime and the available processor cores.Parallelism: Whether to execute in parallel (i.e. whether it is executed on multiple processor cores simultaneously) depends on the runtime environment and the load of the current system. If the system has multiple processor cores and the number of goroutines is large enough that they cannot all execute efficiently on one core, Go's scheduler may distribute these goroutines to be executed in parallel on different cores. However, for this particular piece of code, because the work within goroutine is very light (only printing an empty string), the parallel benefits are not great, and it may even be executed quickly in sequence on one core, because starting goroutine and printing operations are very fast.
To sum up, this code shows concurrent execution, and parallelism depends on the specific situation at the runtime, but for this simple example, parallelism may not be obvious.
This is the end of this article about understanding Go concurrency and parallelism. For more related Go concurrency and parallel content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!