当前位置:网站首页>go 条件变量
go 条件变量
2022-07-02 22:07:00 【给我一瓶冰阔洛】
条件等待和互斥锁有不同,互斥锁是不同协程公用一个锁,条件等待是不同协程各用一个锁,但
是wait()方法调用会等待(阻塞),直到有信号发过来,不同协程是共用信号。
Wait() 阻塞当前协成
func (c *Cond) Wait() {
c.checker.check()
t := runtime_notifyListAdd(&c.notify) // 等待的goruntine数+1
c.L.Unlock() // 释放锁资源
runtime_notifyListWait(&c.notify, t) // 阻塞,等待其他goruntine唤醒
c.L.Lock() // 获取资源
}
Signa() 和 BroadCast() 唤醒协成
func (c *Cond) Signal() {
c.checker.check()
runtime_notifyListNotifyOne(&c.notify) // 唤醒最早被阻塞的goruntine
}
func (c *Cond) Broadcast() {
c.checker.check()
runtime_notifyListNotifyAll(&c.notify) // 唤醒所有goruntine
}
demo
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var cond sync.Cond
//生产者
func producer(out chan<- int, index int){
for{
cond.L.Lock()
for len(out) == 10 {
fmt.Println(index, "len == 10")
cond.Wait()//阻塞 等待
}
num := rand.Intn(800)
time.Sleep(time.Second)
out<- num
fmt.Println("生产者:",index, num)
cond.L.Unlock()
cond.Signal()//唤醒阻塞的协程
}
}
//消费者
func consumer(in <-chan int, index int){
for{
cond.L.Lock()
for len(in) == 0 {
fmt.Println(index, "len == 0")
cond.Wait()//阻塞 等待
}
time.Sleep(time.Second)
num := <-in
fmt.Println("消费者:",index,num)
cond.L.Unlock()
cond.Signal()//唤醒阻塞的协程
}
}
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
}
边栏推荐
- Unity publishes a method of webgl playing sound
- `${}`的用法
- 开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
- How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
- UE4 游戏架构 学习笔记
- [foreign journal] sleep and weight loss
- 'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
- 附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
- Unity发布WebGL播放声音的一种方法
- 解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
猜你喜欢
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
[LeetCode] 数组中的第K个最大元素【215】
phpcms实现订单直接支付宝支付功能
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 cursor
Socket socket c/s end process
Mathematical modeling -- graph and network models and methods (I)
[shutter] shutter application theme (themedata | dynamic modification theme)
[foreign journal] sleep and weight loss
Wait to solve the zombie process
随机推荐
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
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
SimpleITK使用——4. 奇怪的問題
位的高阶运算
杰理之样机无触摸,拆机之后重新安装变正常【篇】
Pointer and string
Jatpack------LiveData
[QT] QT multithreading development - reentrancy and thread safety
Unity3d learning notes 4 - create mesh advanced interface
杰理之如何测试按键的误触率【篇】
Socket socket c/s end process
Source code analysis - lightweight asynchronous crawler framework Ruia
'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
UE4 游戏架构 学习笔记
Pointer array parameter passing, pointer parameter passing
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
Simpleitk use - 3 Common operations
Market Research - current market situation and future development trend of high tibial osteotomy plate
phpcms实现订单直接支付宝支付功能
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #