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()
}
}
In this case out put "ops: 0 qps: 0" every second, why canot read ops in goroutine ?
but when add runtime.Gosched(), everything is ok!
Can every body help me?