共计 653 个字符,预计需要花费 2 分钟才能阅读完成。
download:轻松实现 Rust 零碎入门,实战编译器开发
`package main
import (
“fmt”
“sync”
“time”
)
func ReadDB(wg sync.WaitGroup, Chsignsl chan int, rm sync.RWMutex) {
Chsignsl <- 123
rm.RLock()
fmt.Println(“reading database….”)
<-time.After(1)
rm.RUnlock()
<-Chsignsl
wg.Done()
}
func WriteDB(wg sync.WaitGroup, Chsignsl chan int, rm sync.RWMutex) {
Chsignsl <- 123
rm.Lock()
fmt.Println(“writting database….”)
timer := time.NewTicker(time.Second)
for i := 0; i < 1; i++ {
<-timer.C
}
rm.Unlock()
<-Chsignsl
wg.Done()
}
func main() {
var wg sync.WaitGroup
var rmt sync.RWMutex
Chsignsl := make(chan int, 5)
for i := 0; i < 10; i++ {
for i := 0; i < 3; i++ {wg.Add(1)
go ReadDB(&wg, Chsignsl, &rmt)
}
wg.Add(1)
go WriteDB(&wg, Chsignsl, &rmt)
}
wg.Wait()
fmt.Println(“ 主协程结束 …”)
}
正文完