github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/test/fixedbugs/issue19182.go (about) 1 // run 2 // +build !js 3 4 // Copyright 2017 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package main 9 10 import ( 11 "fmt" 12 "runtime" 13 "sync/atomic" 14 "time" 15 ) 16 17 var a uint64 = 0 18 19 func main() { 20 runtime.GOMAXPROCS(2) // With just 1, infinite loop never yields 21 22 go func() { 23 for { 24 atomic.AddUint64(&a, uint64(1)) 25 } 26 }() 27 28 time.Sleep(10 * time.Millisecond) // Short sleep is enough in passing case 29 i, val := 0, atomic.LoadUint64(&a) 30 for ; val == 0 && i < 100; val, i = atomic.LoadUint64(&a), i+1 { 31 time.Sleep(100 * time.Millisecond) 32 } 33 if val == 0 { 34 fmt.Printf("Failed to observe atomic increment after %d tries\n", i) 35 } 36 37 }