1 Star 0 Fork 0

暗夜之学 / gostudy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 460 Bytes
一键复制 编辑 原始数据 按行查看 历史
hanjian 提交于 2020-12-25 00:04 . study
package main
import (
"fmt"
"sync"
"sync/atomic"
"time"
)
var a, b int64
func main() {
done := make(chan bool, 1)
var mu sync.Mutex
go func() {
for {
select {
case <-done:
return
default:
mu.Lock()
time.Sleep(100 * time.Microsecond)
atomic.AddInt64(&a, 1)
mu.Unlock()
}
}
}()
for i := 0; i < 100000; i++ {
atomic.AddInt64(&b, 1)
mu.Lock()
mu.Unlock()
}
done <- true
fmt.Printf("a = %d b = %d", a, b)
}
Go
1
https://gitee.com/lovehei/gostudy.git
git@gitee.com:lovehei/gostudy.git
lovehei
gostudy
gostudy
73c713180039

搜索帮助