当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
Idea view View the class file idea Class folder
Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
【opencv450】椒盐噪声demo
Convert JSON file of labelme to coco dataset format
Analysis of graphical level-1 programming problem of Electronic Society: cat and mouse
Un million de bonus vous attend, le premier concours d'innovation et d'application de la Chine Yuan cosmique Joint Venture Black Horse Hot Recruitment!
Millions of bonuses are waiting for you to get. The first China Yuan universe innovation and application competition is in hot Recruitment!
js遍历数组(用forEach()方法)
港股今年最大IPO来了,660亿身家,坐在矿山上的“大王”
2021-04-15
Selenium Edge的IE模式
RF analyzer demo setup
The well-known face search engine provokes public anger: just one photo will strip you of your pants in a few seconds
聚合生态,使能安全运营,华为云安全云脑智护业务安全
快速排序的簡單理解
How can genetic testing help patients fight disease?
2021-05-08
MySQL高级语句一
Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
JS创建一个数组(字面量)


![[普通物理] 半波损失 等厚与等倾干涉](/img/66/e0acce623092ecd38e59e38867521d.png)




