当前位置:网站首页>groutine
groutine
2022-04-23 16:54:00 【The interview was rejected 10000 times】
go Concurrent programming
There are two concurrent goroutine At the same time A shared resource , The resulting competitive behavior
1. Using atomic functions
2. Use mutexes
3. Use channel
Concurrency and parallelism

Concurrent Do many things at the same time ( At the same time ), For example, one thread manages multiple goroutine, Multiple... Will be executed in a time period goroutine, In this time period , These tasks may be performed simultaneously , It may not be performed at the same time ( So concurrency is conceptually Include “ parallel ”)
parallel Is to do a lot of things at the same time ( At the same time ), Multiple threads manage multiple threads gotoutine
Multithreaded programs run on a single core , Is the concurrent ; Multithreaded programs run on multiple cores , Is parallel .
Before understanding the concurrency model ( understand goroutine and The difference between threads )
Goroutine: Is a lightweight abstraction based on threads . It allows us to execute multiple functions or methods in parallel in the same address space at a very low cost . Compared to threads , The cost of its creation and destruction is much smaller , And its scheduling is thread independent
- goroutine Different from threads
- Less memory consumption
Goroutine The memory required is usually only 2kb, Threads need 1Mb(500 times ) - Creation and destruction costs less
Goroutine The creation and destruction of are managed by ourselves ,
The creation and destruction of threads is Kernel managed , Consume more - Context switch
Threads are preemptive , The thread execution is completed in a period of time , Other threads preempt cpu, You need to save a lot of thread state , Used to resume the thread when executing again ,
goroutine The scheduling is collaborative , No need to cut into the kernel , Only a small amount of register information needs to be saved and restored
go Of CSP Concurrency model
Two forms of concurrency :
“ Do not use shared memory for communication , To use communication to share memory ”
- Multithreaded shared memory
Common thread concurrency model , Thread communication uses shared memory , Accessing shared data ( For example, an array of ,Map, Or a structure or object ) Using locks ( The mutex , spinlocks , Read-write lock , Atomic lock ) To visit . - csp Model , Use communication to share memory
adopt goroutine and channel To communication-
goroutine yes go Of Concurrent execution unit , Generate a goroutine
go func()
-
channel yes goroutine Communication mechanism between , Namely goroutine Pipeline between
ch:= make(chan type)
-
Concurrency model code :
func send(wg *sync.WaitGroup,send chan<- int){
defer wg.Done()
send <- 1
}
func received(wg *sync.WaitGroup,received <-chan int){
defer wg.Done()
<- received
}
func main(){
ch:= make(chan int)
var wg sync.WaitGroup
go send(&wg,ch)
go received(&wg,ch)
wg.wait()
}
Go Implementation principle of concurrency model (MPG Scheduling model )
Multithreading model
- M:1 ( Multiple user level threads Corresponding A kernel thread )
M( Multiple user level threads ) : This kind of management mode is managed in user mode , There is a thread library , Contains the creation of threads , Dispatch , Manage and destroy . In this thread library, in addition to the normal task threads , There is also a thread dedicated to thread scheduling . Synchronization of threads , Switch Such work is done by yourself
1( Kernel thread ): For the operating system kernel , Not aware of the existence of threads , Perception is a process , I don't know the existence of multithreading
Model advantages :
- flexibility : Because the operating system does not know the existence of threads , So it can be applied to any operating system
- Fast thread switching : Switch in user mode , Enter the kernel state out of order
- No need to modify the operating system : Implement a simple
Model shortcomings :
-
Programming becomes weird : Because user threads need to cooperate with each other to run , Consider when to write such a program CPU For other threads
-
Poor robustness : In the process of execution , If a thread is blocked , He said he couldn't hand over control , The whole process cannot move forward

-
1:1 ( A user thread corresponds to a kernel thread )
Kernel level threads , Thread creation , Dispatch , management , Destruction is implemented in the kernel , In this way, the operating system maintains both process control block and thread control block
Model advantages
User programming is simple : Because the complexity of threads is borne by the system
Model shortcomings
- Low efficiency : Every thread switch requires the kernel , Scheduled by the operating system
- Occupy scarce kernel resources : If kernel space overflows , The operating system will stop

3.M:N ( Two level threading model )
This model Between user level threads and Between kernel level threading models , A process Corresponding Multiple kernel level threads , But the threads in the process Don't Kernel threads correspond one by one .
The model will first create multiple kernel level threads , Then create multiple kernel level threads with their own user level threads , Its own thread is scheduled by the user thread itself , Kernel threads are scheduled by the operating system kernel

> go The thread of MPG Model is a special two-level threading model
Go Thread implementation model MPG
M refer to Machine, One M Directly associated with a kernel thread . Managed by the operating system .
P refer to **”processor”**, Logic processor , On behalf of M The required context , It's also a processor that handles user level code logic . Will wait to be executed G And M docking .Go The runtime system will make P Different from M To establish or disassociate , In order to make P Those in the can run G Be able to get the operation opportunity in time .
(Processor) The number of is set to the environment variable at startup GOMAXPROCS Value , Or call the function at run time runtime.GOMAXPROCS() Set it up
G refer to Goroutine, In fact, it is also a lightweight thread in essence . Including call stack , Important scheduling information .

-
One M Will correspond to a kernel thread , One M It will also connect a context P, A context P Equivalent to one “ processor ”, A context connects one or more Goroutine.
-
P The number of is determined by GOMAXPROCS decision , Generally speaking, it corresponds to the number of cores , For example, in 4Core Your server started back 4 Threads .G There will be many , Every P Will Goroutine From a ready queue Pop operation , To reduce lock competition , Usually every P Will be in charge of a queue .
-
In the figure P Ongoing Goroutine It's blue ; In a pending state Goroutine It's gray , gray Goroutine Formed a queue runqueues
Scheduling logic

goroutine Blocking :
A very simple example is the system call sysall, A thread certainly cannot execute code and system calls at the same time. It is blocked , This is the time , This thread M Need to abandon the current context P, So that others can Goroutine Is scheduled to execute .
As shown in the left figure above ,M0 Medium G0 Yes syscall, Then you create a M1( It may also exist in itself , Not created ),( Turn to the right ) then M0 Discarded P, wait for syscall The return value of ,M1 To accept the P, take · Carry on Goroutine Other in the queue Goroutine.
When the system calls syscall After the end ,M0 Meeting “ steal ” A context , If it doesn't work ,M0 Just put it Gouroutine G0 Put it in a global runqueue in , Then put yourself into the thread pool or go to sleep . overall situation runqueue It's the individual P After running your own local Goroutine runqueue Used to pull new goroutine The place of .P It will also periodically check the global runqueue Upper goroutine, otherwise , overall situation runqueue Upper goroutines May starve to death without execution .
overall situation goroutine

Context P Will check regularly Overall goroutine Queue goroutine, So that they can consume Oneself Goroutine queue I have something to do when I'm busy . If the overall situation goroutine Queue goroutine It's gone ? From other running P Of runqueue Steal in .
Every P Medium Goroutine Different results in different efficiency and time of their operation , There are a lot of P and M In the environment of , You can't let one P Run your own Goroutine There's nothing to do , Because maybe something else P There is a long goroutine Run in line , Need to be balanced .
How to solve it ?
Go It's also direct , From the other P Steal half of !
版权声明
本文为[The interview was rejected 10000 times]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231359046258.html
边栏推荐
- feign报400处理
- 文件操作详解(2)
- How to implement distributed locks with redis?
- ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
- True math problems in 1959 college entrance examination
- Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
- Zhimeng dedecms security setup Guide
- ∑GL-透视投影矩阵的推导
- Smart doc + Torna generate interface document
- Flask如何在内存中缓存数据?
猜你喜欢

Shell脚本——Shell编程规范及变量

如何建立 TikTok用户信任并拉动粉丝增长

How to choose the wireless gooseneck anchor microphone and handheld microphone scheme

Use if else to judge in sail software - use the title condition to judge

The font of the soft cell changes color

Construction of promtail + Loki + grafana log monitoring system

Solution of garbled code on idea console

File upload and download of robot framework

PyTorch:train模式与eval模式的那些坑

NVIDIA显卡驱动报错
随机推荐
Idea of batch manufacturing test data, with source code
Disk management and file system
PyTorch:train模式与eval模式的那些坑
RTKLIB 2.4.3源码笔记
Calculation formula related to tolerance analysis
Linux MySQL data timing dump
无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
How to implement distributed locks with redis?
Log4j output log information to file
The new MySQL table has a self increasing ID of 20 bits. The reason is
Deepinv20 installation MariaDB
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
人脸识别框架之dlib
Derivation of Σ GL perspective projection matrix
Construction of promtail + Loki + grafana log monitoring system
伪分布安装spark
正则过滤内网地址和网段
Copy constructor shallow copy and deep copy
Knowledge points and examples of [seven input / output systems]
New project of OMNeT learning