当前位置:网站首页>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
边栏推荐
猜你喜欢

重写YOLOX的TensorRT版本部署代码

Emotional classification based on Bert

Summary of multimodal fusion methods

2021-11-17 ESP32引脚参考

Geo_ CNN (tensorflow version)

7 kinds of visual MLP finishing (Part 1)

Impact of static keyword on scope and lifecycle

Database design of Internet cafe management system

A Survey of Robust LiDAR-based 3D Object Detection Methods for Autonomous Driving(激光雷达3D目标检测方法)论文笔记

如何用NumPy读取和保存点云数据
随机推荐
QuizCardGame
sd nand与nandflash的区别?
golang高并发特性goroutine介绍
隔离4-20mA或0-20mA信号传输
压力应变桥信号处理光电隔离放大器
5种2D Attention整理(Non-Local、Criss-Cross、SE、CBAM、Dual-Attention)
Geo_ CNN (tensorflow version)
5种3D Attention/Transformer整理(A-SCN、Point Attention、CAA、Offset Attention、Point Transformer)
新产品如何选型Flash?
Review of software process and management (VII)
【CS创世】 SD NAND和Raw NAND优劣势对比分析
SD NAND与eMMC优劣势对比
深度聚类相关(三篇文章)
深度学习中常用的激活函数
HRA 1~12W 系列12V《宽压9~18V》转±250VDC等升压变换器
4-20ma转4-20ma 0-5v转0-5v 模拟信号隔离变送器
LTH7五脚芯片的完整方案图FS4054充电电路原理
2021-11-26 pyautogui 配合雷电模拟器实现手机APP签到答题自动化
HT7727 HT7730 HT7733 HT7737 HT7750异步DCDC升压IC
基于复旦微 FPGA+华为海思 HI3531DV200+兆易创新MCU 的综合视频处理卡