当前位置:网站首页>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

 reference go Language practice

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
  1. Less memory consumption
    Goroutine The memory required is usually only 2kb, Threads need 1Mb(500 times )
  2. 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
  3. 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
    1. goroutine yes go Of Concurrent execution unit , Generate a goroutine

      go func()

    2. 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

  1. 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

  1. flexibility : Because the operating system does not know the existence of threads , So it can be applied to any operating system
  2. Fast thread switching : Switch in user mode , Enter the kernel state out of order
  3. No need to modify the operating system : Implement a simple

Model shortcomings

  1. 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

  2. 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
     User level threading model

  3. 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

  1. Low efficiency : Every thread switch requires the kernel , Scheduled by the operating system
  2. Occupy scarce kernel resources : If kernel space overflows , The operating system will stop

 Kernel level threading model

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

 Two level threading model

> 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 .

MPG Thread scheduling model

  1. 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.

  2. 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 .

  3. 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

 Thread blocking scheduling diagram

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

 Idle processor scheduling

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 !

Picture content reference link

Reference links

Reference links

版权声明
本文为[The interview was rejected 10000 times]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231359046258.html