当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
PHP 2D array insert
js的slice()和splice()
golang 重要知识:atomic 原子操作
重卡界销售和服务的“扛把子”,临沂广顺深耕产品全生命周期服务
乐高宣布涨价,炒家更嗨皮了
Mysql database - log management, backup and recovery
2021-05-08
[普通物理] 半波损失 等厚与等倾干涉
labelme的JSON文件转成COCO数据集格式
Golang -- multiple processing scenarios for files
从3开始,在业务系统中增加分页功能
golang 重要知识:waitgroup 解析
Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
The work and development steps that must be done in the early stage of the development of the source code of the live broadcasting room
php 二维数组插入
How to solve the problem that iterative semi supervised training is difficult to implement in ASR training? RTC dev Meetup
百万奖金等你来拿,首届中国元宇宙创新应用大赛联合创业黑马火热招募中!
golang--判断字符串是否相等
详解Redis分布式锁的原理与实现
腾讯云服务器发送邮件失败








