package main
import "fmt"
import "time"
import (
"runtime"
"sync/atomic"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
var t1 = time.Now()
var ops uint64 = 0
go func() {
for {
time.Sleep(time.Second)
opsFinal := atomic.LoadUint64(&ops)
fmt.Println("ops:", opsFinal, "qps:", opsFinal/uint64(time.Since(t1).Seconds()))
}
}()
for {
atomic.AddUint64(&ops, 1)
//runtime.Gosched()
}
}
在这种情况下,每秒输出“ ops:0 qps:0”,为什么不能在goroutine中读取ops?
但是当添加runtime.Gosched()时,一切正常!
每个人都可以帮助我吗?
萧十郎
jeck猫
相关分类