...
Run Format

Source file test/fixedbugs/issue19182.go

Documentation: test/fixedbugs

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

View as plain text