当前位置:网站首页>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
边栏推荐
- Decimal format decimal / datetime conversion processing
- Construction of promtail + Loki + grafana log monitoring system
- Camtasia2022软件新增功能介绍
- Expression "func" tSource, object "to expression" func "tSource, object" []
- Server log analysis tool (identify, extract, merge, and count exception information)
- Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
- Encapsulating the logging module
- 拷贝构造函数 浅拷贝与深拷贝
- Calculation formula related to tolerance analysis
- Nodejs installation and environment configuration
猜你喜欢
How vscode compares the similarities and differences between two files
DanceNN:字节自研千亿级规模文件元数据存储系统概述
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
SQL database
众昂矿业:萤石浮选工艺
How much do you know about the process of the interview
Disk management and file system
Real time operation of vim editor
True math problems in 1959 college entrance examination
feign报400处理
随机推荐
How much do you know about the process of the interview
Zhimeng dedecms security setup Guide
Log4j output log information to file
【题解】[SHOI2012] 随机树
feign报400处理
Camtasia2022软件新增功能介绍
UWA Pipeline 功能详解|可视化配置自动测试
Selenium IDE and XPath installation of chrome plug-in
Paging the list collection
计组 | 【七 输入/输出系统】知识点与例题
vscode如何比较两个文件的异同
◰ GL shadow map core steps
MySQL modify master database
Sub database and sub table & shardingsphere
◰GL-阴影贴图核心步骤
Pytorch: the pit between train mode and eval mode
文件操作详解(2)
1959年高考数学真题
Zhongang Mining: Fluorite Flotation Process
◰ GL shader handler encapsulation