当前位置:网站首页>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 .
边栏推荐
- golang 重要知识:atomic 原子操作
- A transformer can only convert alternating current. How can I convert direct current?
- 港股今年最大IPO来了,660亿身家,坐在矿山上的“大王”
- 2021-05-08
- 力扣解法匯總513-找樹左下角的值
- Converging ecology, enabling safe operation, Huawei cloud security, cloud brain intelligent service security
- Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)
- JS垃圾回收
- JS里的数组
- AXI_ Round_ Robin_ Arbiter design - aw and W channels
猜你喜欢

WebService interface publishing and calling

js遍历数组(用forEach()方法)

raspberry pi安装 wiringpi

Pop() element in JS

Starting from 3, add paging function in the business system

2021-05-08

Ie mode of selenium edge

The largest IPO of Hong Kong stocks this year, with a net worth of 66billion, is the "King" sitting on the mine

golang 重要知识:waitgroup 解析

2021-06-07
随机推荐
2021-05-08
2021-05-08
MySQL advanced statement I
F5 application strategy status report in 2022: edge deployment and load security become the focus of attention in the Asia Pacific Region
Introduction to the push function in JS
mysql主从只同步部分库或表的思路与方法
[普通物理] 光的衍射
AI intelligent robot saves us time and effort
Solution to the problem that MySQL cannot be started in xampp
Raspberry PI installing the wiring pi
山东:美食“隐藏款”,消费“扫地僧”
Une compréhension simple du tri rapide
mysql 系列:总体架构概述
从3开始,在业务系统中增加分页功能
列表查询排序参数处理
2021-06-03
【云驻共创】制造业企业如何建设“条码工厂”
Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
2021-04-15
Uniswap acquires genie, an NFT transaction aggregator. Will the NFT transaction market change?