当前位置:网站首页>Important knowledge of golang: sync Cond mechanism

Important knowledge of golang: sync Cond mechanism

2022-06-23 15:24:00 yue_ xin_ tech

Preface

stay Go There are special for synchronous communication channel, So we seldom see sync.Cond Use . But it is also one of the concurrency control methods , Today, let's take a look at its implementation , Deepen the use of synchronization mechanism .

sync.Cond

sync.Cond There are three ways :Wait()、Signal()、Broadcast(), They are used as follows :

  • Wait(): Block the current goroutine, Waiting for arousal .
  • Signal(): Evoke a blocked goroutine.
  • Broadcast(): Evoke all blocked goroutine.

Describe by the above method , We can simply implement one Task pool function : Create in batch first goroutine, And then call sync.Cond Of Wait() Method to make it block the wait .

When a task comes , Through Signal() Evoke one that has just been blocked goroutine, To carry out the task .

Through the task pool function , We found that sync.Cond The use of is very simple , but Go We are not officially recommended to use sync.Cond To achieve Synchronous communication between processes .

Because it does not conform to Go official “ Share memory through communication ” Design idea , When the scene is complex , It will couple various business functions .

sync.Cond Source code analysis

Let's see sync.Cond The structure of the body , Code in /sr/sync/cond.go Next :

type Cond struct {
    
	noCopy noCopy 		//  Do not copy 
	L Locker			//  lock 
	notify  notifyList	//  Notification call list 
	checker copyChecker //  Copy detection 
}

You can see Cond There are notify list , And this is exactly what needs to be aroused goroutine list .

When we call Wait() Method will maintain the current goroutine To the corresponding notifyList in :

func (c *Cond) Wait() {
    
	c.checker.check()
	t := runtime_notifyListAdd(&c.notify) //  Will the current  goroutine  Add to  notifyList  in 
	c.L.Unlock()
	runtime_notifyListWait(&c.notify, t) //  Block waiting 
	c.L.Lock()
}

When other coroutines call Signal or Broadcast When the method is used , Will pass runtime_notifyListNotifyOne or runtime_notifyListNotifyAll Method to invoke one or more goroutine.

Implementation of other synchronization methods

As mentioned earlier sync.Cond Not recommended as a means of collaborative communication , If we want to realize its unicast 、 Broadcast effect , How to do it ?

It's also very simple , If we want to achieve unicast effect , Then you just need to listen through blocking channel The signal is good .

If you want to achieve the effect of broadcast arousal , Just use context Chain cancellation feature of , This effect can also be achieved .


Interested friends can search the official account 「 Read new technology 」, Pay attention to more push articles .
If you can , Just like it by the way 、 Leave a message 、 Under the share , Thank you for your support !
Read new technology , Read more new knowledge .
 Read new technology

原网站

版权声明
本文为[yue_ xin_ tech]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231438381072.html