Source file test/fixedbugs/issue19182.go

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

View as plain text