当前位置:网站首页>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
}
边栏推荐
- [micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
- 高并发介绍及应对
- Using rendertext() to output multiple lines of text with rendertext() in R shiny
- 原生js添加样式的方法
- Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
- Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
- 服务器响应状态码
- #include errors detected. Please update your includePath.
- [autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
- Market Research - current situation and future development trend of herringbone gear Market
猜你喜欢
任务和特权级保护
[LeetCode] 多数元素【169】
悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
NC50965 Largest Rectangle in a Histogram
Struct, bit segment, enumeration, union
Mathematical modeling -- graph and network models and methods (I)
数组进阶提高
【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
Oracle cursor
随机推荐
Market Research - current market situation and future development trend of genome editing mutation detection kit
Pointer - function pointer
Market Research - current situation and future development trend of herringbone gear Market
[QT] QT multithreading development - reentrancy and thread safety
杰理之快速触摸不响应问题【篇】
Build your own website (22)
影视随摘
电商系统微服务架构
SimpleITK使用——3. 常见操作
Market Research - current market situation and future development trend of high tibial osteotomy plate
go 4种单例模式
杰理之内置短按再长按,不管长按多长时间都是短按【篇】
钟薛高回应产品1小时不化:含固体成分 融化不能变成水
高并发介绍及应对
Simpleitk use - 3 Common operations
Radis:Linux上安装Redis(步骤)
【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
U++ 学习笔记 ----松弛
uniapp微信登录返显用户名和头像
Source code analysis - lightweight asynchronous crawler framework Ruia