Home > Common Problem > body text

What does concurrency mean at the go language level?

DDD
Release: 2023-06-13 09:40:54
Original
923 people have browsed it

Concurrency at the go language level means using the go language to perform multiple tasks within the same time period. Concurrency in the Go language is achieved through goroutine. Goroutine is similar to a thread and belongs to user mode threads. Thousands can be created as needed. Tens of thousands of goroutines work concurrently.

What does concurrency mean at the go language level?

#The operating environment of this article: Windows 10 system, go1.20 version, dell g3 computer.

Concurrency at the go language level means using the go language to perform multiple tasks at the same time.

Concurrency is a very important concept in programming. Go language inherently supports concurrency at the language level.

Concurrency and parallelism

Concurrency: perform multiple tasks within the same time period (for example: chatting with two girlfriends using WeChat).

Parallel: Perform multiple tasks at the same time (for example: you and your friends are chatting with your girlfriend on WeChat).

Concurrency in Go language is realized through goroutine. Goroutine is similar to thread (thread and process are concepts derived from the operating system. A process is equivalent to a large workshop, and the CPU is equivalent to a factory. There are many workshops in a factory. , the process divides the factory into workshops. Threads are divided into processes, such as workers and various resources on the process. There is at least one thread in a process.), threads belonging to user mode, we can create them as needed Thousands of goroutines work concurrently. Goroutine is scheduled by the runtime of the Go language, and the second thread is scheduled by the operating system.

The Go language also provides channels to communicate between multiple goroutines. Goroutine and channel are an important implementation basis for the Go language to adhere to the CSP (promoting communication through shared memory rather than shared memory) concurrency mode

Extension:

goroutine

Similar to threads, implemented at the language level and running on operating system threads.

A goroutine must correspond to a function, and multiple goroutines can be created to execute the same function.

It is very convenient to use goroutine in go. When calling a function, add the go keyword in front to create a goroutine for a function.

Start a single goroutine:

package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup //WaitGroup等待方法
// goroutine demo
func hello(i int){
fmt.Println("Hello hello",i)
wg.Done() //此方法如果运行 ,通知wg把计数器 -1
}
func main() {  // 开启一个主goroutine去执行mian函数
wg.Add(10000)//(计数器)只有一个小弟为1,等待wg.Done()后-1,为0时停止等待
for i:=0; i<10000;i++{
//wg.Add(1) 可以给定10000个goroutine 也可以,每次循环+1
go hello(i) // 开启了一个独立的 goroutine去执行hello这个函数
}
fmt.Println("Hello main")
// 让我们的主goroutine 等待 goroutine 小弟 执行
// 如果不等待,独立的goroutine小弟,可能小弟这个goroutine还没有运行
//time.Sleep(time.Second) 第二种等待
wg.Wait()//等待所有小弟干完活
}
Copy after login

The difference between goroutine and thread

OS thread (operating system thread) has a fixed stack memory ( Usually 2MB), a goroutine's stack has only a small stack at the beginning of its life cycle (typically 2KB). The goroutine's stack is not fixed. It can grow and shrink as needed. The stack size limit of a goroutine can reach 1GB. , but rarely up to 1GB. Therefore, it is possible to create about 100,000 grorutines at one time in the Go language.

The above is the detailed content of What does concurrency mean at the go language level?. 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