当前位置:网站首页>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
}
边栏推荐
- 杰理之修改不需要长按开机功能【篇】
- Oracle-PL/SQL编程
- NC24325 [USACO 2012 Mar S]Flowerpot
- Server response status code
- 位的高阶运算
- #include errors detected. Please update your includePath.
- Market Research - current market situation and future development trend of aircraft front wheel steering system
- New feature of go1.18: trylock, which has been tossed n times
- 【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
- Pointer - function pointer
猜你喜欢

世界环境日 | 周大福用心服务推动减碳环保

对象与对象变量

【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)

Source code analysis - lightweight asynchronous crawler framework Ruia
![[001] [arm-cortex-m3/4] internal register](/img/49/a0eceac1a67267216dd9b2566033a1.jpg)
[001] [arm-cortex-m3/4] internal register

PMP项目整合管理

Objects and object variables

性能优化----严苛模式

Perceptron model and Application

Jatpack------LiveData
随机推荐
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
Market Research - current situation and future development trend of cell-based seafood market
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
New feature of go1.18: trylock, which has been tossed n times
[shutter] shutter application theme (themedata | dynamic modification theme)
New feature of go1.18: introduce new netip Network Library
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
性能优化----严苛模式
Golang的学习路线
Socket套接字C/S端流程
小鹏P7出事故,安全气囊未弹出,这正常吗?
位的高阶运算
Market Research - current situation and future development trend of marine clutch Market
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
Oracle-PL/SQL编程
牛客网:龙与地下城游戏
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
任务和特权级保护
[LeetCode] 数组中的第K个最大元素【215】
go 条件变量