package main
import (
"fmt"
"strconv"
"sync"
)
var (
s [2]map[string]string
current = 0
wg sync.WaitGroup
)
func w(s1 string) {
tmpIndex := (current + 1) % 2
if s[tmpIndex] == nil {
s[tmpIndex] = make(map[string]string)
}
s[tmpIndex][s1] = s1
current = tmpIndex
fmt.Println(current)
}
func r() {
for i := 0; i < 100; i++ {
//fmt.Println(current, s[current])
}
}
func main() {
w("1")
r()
wg.Add(2)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
//time.Sleep(5)
r()
}
}()
go func() {
defer wg.Done()
for i := 0; i < 5; i++ {
//time.Sleep(5)
w(strconv.Itoa(i))
}
}()
wg.Wait()
}
双buffer防止 map读写并发
最新推荐文章于 2024-08-12 14:50:43 发布