In short, the so-called concurrent programming refers to processing multiple tasks "simultaneously" on one processor.
In the Golang language, the mechanism of coroutines used concurrently is also very convenient to implement. Just use the go keyword. (Recommended learning: go)
func main() { ... go fun(){ fmt.Println("Hi, Here is a goroutine.") }() ... }
What is Goroutine
Goroutine is the core of Go parallel design. In the final analysis, goroutine is actually a coroutine, which is smaller than a thread. A dozen goroutines may be reflected in five or six threads at the bottom. The Go language helps you realize memory sharing among these goroutines.
Executing goroutine requires very little stack memory (about 4~5KB), and of course it will scale according to the corresponding data. Because of this, thousands of concurrent tasks can be run simultaneously. Goroutine is easier to use, more efficient, and lighter than thread.
Under normal circumstances, an ordinary computer running dozens of threads is a bit overloaded, but the same machine can easily allow hundreds or thousands of goroutines to compete for resources.
Creation of Goroutine
Just add the go keyword before the function call statement to create a concurrent execution unit. Developers do not need to know any execution details, the scheduler will automatically arrange it for execution on the appropriate system thread.
In concurrent programming, we usually want to divide a process into several pieces, and then let each goroutine be responsible for a piece of work. When a program starts, the main function runs in a separate goroutine, which we call Its main goroutine. New goroutines will be created using go statements.
The concurrency design of go language allows us to achieve this goal easily
The above is the detailed content of What is golang concurrent programming?. For more information, please follow other related articles on the PHP Chinese website!