当前位置:网站首页>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
}
边栏推荐
- Oracle-游标
- Introduction to database system Chapter 1 short answer questions - how was the final exam?
- Market Research - current market situation and future development trend of intravenous injection (IV) bottles
- 性能优化----严苛模式
- UE4 UI自适应屏幕
- 数据库系统概论第一章简答题-期末考得怎么样?
- Market Research - current market situation and future development trend of third-party data platform
- U++ 学习笔记 堆
- 牛客网:最大子矩阵
- Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
猜你喜欢
Perceptron model and Application
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
[LeetCode] 多数元素【169】
Objects and object variables
Promise optimized callback hell
杰理之、产线装配环节【篇】
PMP项目整合管理
Wait to solve the zombie process
Pointer and string
电商系统微服务架构
随机推荐
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
kubernetes 使用主机名将 pod 分配在指定节点上
杰理之内置短按再长按,不管长按多长时间都是短按【篇】
JS获取display为none的隐藏元素的宽度和高度的解决方案
小鹏P7出事故,安全气囊未弹出,这正常吗?
U++ learning note pile
Phpcms realizes the direct Alipay payment function of orders
Pointer and string
[LeetCode] 回文数【9】
New feature of go1.18: trylock, which has been tossed n times
PHP wechat red packet grabbing algorithm
#include errors detected. Please update your includePath.
Market Research - current market situation and future development trend of night vision goggles for pilots
[foreign journal] sleep and weight loss
Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
性能优化----严苛模式
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
[shutter] shutter resource file use (import resource pictures | use image resources)
php实现根据输入的年龄查询出生日期符合的数据
Source code analysis - lightweight asynchronous crawler framework Ruia