标签:code write make add lock tab imp base pack
`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("主协程结束...")
}
`
标签:code write make add lock tab imp base pack
原文地址:https://www.cnblogs.com/warrior-tian/p/13060437.html