What is golang concurrent programming?

(*-*)浩
Release: 2019-12-13 14:16:18
Original
1945 people have browsed it

What is golang concurrent programming?

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.")
    }()
    ...
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template