当前位置:网站首页>Go condition variable
Go condition variable
2022-07-02 22:50:00 【Give me a bottle of Borneol】
Conditional waiting is different from mutexes , A mutex is a lock shared by different processes , Conditional waiting is that different processes each use a lock , but
yes wait() Method calls wait ( Blocking ), Until a signal comes , Different co processes are common signals .
Wait() Block the current collaboration
func (c *Cond) Wait() {
c.checker.check()
t := runtime_notifyListAdd(&c.notify) // Waiting for the goruntine Count +1
c.L.Unlock() // Release lock resource
runtime_notifyListWait(&c.notify, t) // Blocking , Waiting for others goruntine Wake up the
c.L.Lock() // Access to resources
}
Signa() and BroadCast() Wake up synergy
func (c *Cond) Signal() {
c.checker.check()
runtime_notifyListNotifyOne(&c.notify) // Wake up the first to be blocked goruntine
}
func (c *Cond) Broadcast() {
c.checker.check()
runtime_notifyListNotifyAll(&c.notify) // Wake up all goruntine
}
demo
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var cond sync.Cond
// producer
func producer(out chan<- int, index int){
for{
cond.L.Lock()
for len(out) == 10 {
fmt.Println(index, "len == 10")
cond.Wait()// Blocking wait for
}
num := rand.Intn(800)
time.Sleep(time.Second)
out<- num
fmt.Println(" producer :",index, num)
cond.L.Unlock()
cond.Signal()// Wake up the blocked process
}
}
// consumer
func consumer(in <-chan int, index int){
for{
cond.L.Lock()
for len(in) == 0 {
fmt.Println(index, "len == 0")
cond.Wait()// Blocking wait for
}
time.Sleep(time.Second)
num := <-in
fmt.Println(" consumer :",index,num)
cond.L.Unlock()
cond.Signal()// Wake up the blocked process
}
}
func main() {
ch := make(chan int, 10)
rand.Seed(time.Now().UnixMilli())
cond.L = new(sync.Mutex)
for i:=1; i<=4; i++{
go producer(ch, i)
}
for i:=1; i<=6; i++{
go consumer(ch, i)
}
quit := make(chan []struct{})
<-quit
}
边栏推荐
- Market Research - current situation and future development trend of environmental friendly fireworks Market
- Oracle-PL/SQL编程
- 杰理之内置关机电流 1.2uA,之后不能长按开机【篇】
- [autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
- Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
- Market Research - current market situation and future development trend of night vision goggles for pilots
- Perceptron model and Application
- Simpleitk use - 4 Strange question
- Phpcms realizes the direct Alipay payment function of orders
- Market Research - current market situation and future development trend of third-party data platform
猜你喜欢
Build your own website (22)
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Source code analysis - lightweight asynchronous crawler framework Ruia
Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
世界环境日 | 周大福用心服务推动减碳环保
小鹏P7出事故,安全气囊未弹出,这正常吗?
Dynamic memory allocation (malloc calloc realloc free)
全面解析分享购商业模式逻辑?分享购是如何赋能企业
牛客网:龙与地下城游戏
E-commerce system microservice architecture
随机推荐
U++ 学习笔记 堆
Notes on key vocabulary in the English original of the biography of jobs (10) [chapter eight]
PMP项目整合管理
php实现根据输入的年龄查询出生日期符合的数据
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
[QT] QT multithreading development - reentrancy and thread safety
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
存储单位换算
位的高阶运算
原生js添加样式的方法
【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
Market Research - current situation and future development trend of sickle cell therapy Market
Oracle cursor
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
Market Research - current situation and future development trend of cell-based seafood market
Introduction to database system Chapter 1 short answer questions - how was the final exam?
UE4 game architecture learning notes
UE4 游戏架构 学习笔记
U++ 原始内存 学习笔记
杰理之内置短按再长按,不管长按多长时间都是短按【篇】