当前位置:网站首页>Introduction to goroutine, a high concurrency feature of golang
Introduction to goroutine, a high concurrency feature of golang
2022-07-19 05:56:00 【One, two, three】
1 golang High concurrency goroutine Introduce
List of articles
goroutine yes go The core of language high concurrency design , Is a very lightweight implementation , Thousands of concurrent tasks can be performed in a single process , At its core MPG Scheduling model .
1.1 goroutine Principle introduction
Specifically goroutine Before using the method , First introduce its basic principles and related concepts , If you just want to know how to use , Skip directly to the second section .
First of all, introduce the following concept : Concurrent 、 process 、 Thread and Ctrip .
The concept of concurrency
Long ago , Computers have only one core , The development language metallurgy is to write code logic in sequence , Users can submit one job directly at a time , Computers can only do one thing at a time . But with the development of computer technology , Software design is becoming more and more complex , as well as CPU/ Memory / Hard disk, etc. IO Read and write wait caused by the huge difference in speed , This single process sequential execution , Unable to meet the scenario Application .
In order to make better use of CPU resources , One CPU Perform multiple tasks at the same time , adopt CPU Time slice polling extreme , Realize the switching between tasks , It looks like multitasking in time , This is concurrency .
The time-sharing call of computer is the root of concurrency ,CPU Execute different jobs by quickly switching jobs , When each job is blocked during execution , Release CPU resources , Wait until the dispatching unit is awakened again , It can be used again CPU resources , The operating system ensures the whole scheduling process .process / Threads / Ctrip
A process is an execution of a program in the operating system , An independent unit of the system for resource allocation and scheduling , Process is the smallest unit of computer resource allocation , yes CPU The basic unit of allocation of resources , There is independent memory .
A thread is an execution entity of a process , yes CPU Basic unit of dispatch and dispatch , It's a smaller, independent, basic unit than a process , It is the smallest unit of computer scheduling , A process can have multiple threads
coroutines : Independent stack space , Shared heap space , Scheduling is controlled by the user , It's kind of like a user level thread in essence , The scheduling of these user level threads is also implemented by itself , Its bottom layer is based on thread pool , There is a scheduler on it , On which thread does the scheduling task execute
- goroutine principle
goroutine yes golang The process of realization , Its characteristic is that it supports , It is very convenient to use , At its core MPG Scheduling model (M: Kernel thread 、P: processor , Maintain local running queues 、G:goroutine, Code and data structures 、S: Scheduler , maintain M and P)
M Will associate kernel threads with processes , Context P There are two kinds Goroutine, One is running , Blue in the picture G; One is waiting in line , Gray in the picture G, This will be stored in the process runqueue Inside . The context here P The quantity of also means Goroutinue Number of runs , Through the environment variable GOMAXPROCS Value , Or call the function at run time runtime.GOMAXPROCS() Set it up .
Here is a simple explanation , Specific implementation details , Referable https://zhuanlan.zhihu.com/p/82740001
1.2 goroutine Usage method
golang Used in program go keyword , Add to the front of a function call , You can achieve one goroutine, It's simple , Note here that if you use go establish goroutine when , The return value of the function will be ignored , If you want to realize communication between multiple concurrent ,go Provides channel Mechanism , Very easy to use , It will be introduced separately later .
Use format :
go Function name ( Enter the reference )
Examples are as follows :
package main
import (
"fmt"
"time"
)
func gotest1() {
i := 0
for {
i++
fmt.Printf("gotest1 run: i = %d\n", i)
time.Sleep(3*time.Second) // Time delay 1s
}
}
func main() {
// To start a goroutine
go gotest1()
i := 0
for {
i++
fmt.Printf("main: i = %d\n", i)
time.Sleep(3 * time.Second) // Time delay 1s
}
}
call runtime.Goexit() The current... Will be terminated immediately goroutine Of board ⾏, The scheduler ensures that all are registered defer Deferred calls are executed .
package main
import (
"fmt"
"runtime"
)
func main() {
go func() {
defer fmt.Println("1111")// Execute when the function exits , amount to c++ Destructor of
func() {
defer fmt.Println("2222")
runtime.Goexit() // Terminate the current goroutine
fmt.Println("3333")
}()
fmt.Println(44444")
}() // How to write anonymous functions
for {
}
}
The execution result is
2222
1111
边栏推荐
- 7种视觉MLP整理(下)
- Seq2seq (Chinese English translation) attention
- 2021-05-21
- Contrastive learning for image semantic segmentation (two articles)
- HRA隔离系列 宽电压输入 正负高电压稳压输出
- 4路编码器脉冲计数器,转速测量,8路DO,Modbus TCP数据采集模块
- CUDA编程-02: 初识CUDA编程
- HT7727 HT7730 HT7733 HT7737 HT7750异步DCDC升压IC
- 【CS创世】 SD NAND和Raw NAND优劣势对比分析
- 无线充发光鼠标垫RGB LED照明无线充电鼠标垫
猜你喜欢

FS4061A(5V USB输入、双节锂电池串联应用、5v升压充电8.4v管理IC

Review of software process and management (VIII)

FMC子卡:8 通道 125MSPS采样率16 位 AD 采集子卡

Run yolov5 process record based on mindspire

HM9922开关降压型 LED恒流驱动器IC

4 路 FMC+基带信号处理板( 4 路 2G 瞬时带宽 AD+DA)

7种视觉MLP整理(下)

Record: yolov5 model pruning lightweight

Sgm: sequence generation model for multi label classification

CUDA编程-03:线程层级
随机推荐
pcie CameraLink信号发生器(CameraLink图像模拟源)
3U VPX导冷高性能SRIO/以太网数据交换板
DAC7512N 模拟混合信号IC转换器
深度学习中常用的激活函数
MCU单片机OTP
升压DC/DC转换器
Xinlinx zynq7020、7045国产替代 FMQL45T900 全国产化 ARM 核心板+扩展板
5种2D Attention整理(Non-Local、Criss-Cross、SE、CBAM、Dual-Attention)
Pytorch learning notes [5]: generalization using convolution
如何用TensorRT部署YOLOv6
Face detection based on OpenCV and face interception
go语言介绍及应用场景分析
Review of software process and management (VII)
Overview of self supervised learning
新产品如何选型Flash?
7 kinds of visual MLP finishing (Part 1)
Deep clustering correlation (three articles)
Impact of static keyword on scope and lifecycle
2021-11-17 ESP32引脚参考
一文读懂目标检测中的各种IoU损失函数