当前位置:网站首页>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
}
边栏推荐
- phpcms实现订单直接支付宝支付功能
- Market Research - current market situation and future development trend of night vision goggles for pilots
- Notes on key vocabulary in the English original of the biography of jobs (10) [chapter eight]
- 杰理之如何测试按键的误触率【篇】
- 手写ORM(对象关系映射)增删改查
- Market Research - current market situation and future development trend of marine wet exhaust hose
- [LeetCode] 存在重复元素【217】
- PHP optimizes SQL queries in foreach
- 【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
- 分享 10 个 JS 闭包面试题(图解),进来看看你能答对多少
猜你喜欢

数组进阶提高

540. Single element in ordered array

数学建模——图与网络模型及方法(一)

Leetcode circular linked list (fast and slow pointer) code line by line interpretation

Perceptron model and Application

SimpleITK使用——4. 奇怪的問題

悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖

Objects and object variables

Socket socket c/s end process

NC50965 Largest Rectangle in a Histogram
随机推荐
Market Research - current market situation and future development trend of high tibial osteotomy plate
Unity publishes a method of webgl playing sound
Notes on key vocabulary in the English original of the biography of jobs (10) [chapter eight]
【外刊】睡眠与减肥
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
地方经销商玩转社区团购模式,百万运营分享
世界环境日 | 周大福用心服务推动减碳环保
Struct, bit segment, enumeration, union
Market Research - current situation and future development trend of anterior cruciate ligament (ACL) reconstruction Market
JS solution for obtaining the width and height of hidden elements whose display is none
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
用sentinel熔断比例阈值改不了,设置慢调用比例没效果
[LeetCode] 反转字符串中的单词 III【557】
[LeetCode] 反转字符串【344】
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
Simpleitk use - 4 Strange question
Source code analysis - lightweight asynchronous crawler framework Ruia
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
How should programmers write logs
Unity发布WebGL播放声音的一种方法