当前位置:网站首页>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
边栏推荐
- DanceNN:字节自研千亿级规模文件元数据存储系统概述
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- 网络安全之渗透靶场实战详解
- How to implement distributed locks with redis?
- Expression "func" tSource, object "to expression" func "tSource, object" []
- 关于局域网如何组建介绍
- ByteVCharts可视化图表库,你想要的我都有
- Get the column name list of the table quickly in Oracle
- Dlib of face recognition framework
- MySql主从复制
猜你喜欢
STM32__03—初识定时器
Knowledge points and examples of [seven input / output systems]
Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
New project of OMNeT learning
Detailed explanation of the penetration of network security in the shooting range
Sub database and sub table & shardingsphere
The font of the soft cell changes color
Construction of promtail + Loki + grafana log monitoring system
RTKLIB 2.4.3源码笔记
Creation of RAID disk array and RAID5
随机推荐
Linux MySQL data timing dump
织梦DEDECMS安全设置指南
Do you really understand the principle of code scanning login?
TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
批量制造测试数据的思路,附源码
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
Use case labeling mechanism of robot framework
The new MySQL table has a self increasing ID of 20 bits. The reason is
∑GL-透视投影矩阵的推导
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
How to implement distributed locks with redis?
Detailed explanation of UWA pipeline function | visual configuration automatic test
Public variables of robotframework
How vscode compares the similarities and differences between two files
Real time operation of vim editor
Mock test using postman
New project of OMNeT learning
Copy constructor shallow copy and deep copy
杂文 谈谈古典的《拆掉思维里的墙》
英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)