github.com/kidsbmilk/gofronted_all@v0.0.0-20220701224323-6479d5976c5d/libgo/misc/cgo/testsanitizers/testdata/tsan7.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  // Setting an environment variable in a cgo program changes the C
     8  // environment. Test that this does not confuse the race detector.
     9  
    10  /*
    11  #cgo CFLAGS: -fsanitize=thread
    12  #cgo LDFLAGS: -fsanitize=thread
    13  */
    14  import "C"
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  	"sync"
    20  	"time"
    21  )
    22  
    23  func main() {
    24  	var wg sync.WaitGroup
    25  	var mu sync.Mutex
    26  	f := func() {
    27  		defer wg.Done()
    28  		for i := 0; i < 100; i++ {
    29  			time.Sleep(time.Microsecond)
    30  			mu.Lock()
    31  			s := fmt.Sprint(i)
    32  			os.Setenv("TSAN_TEST"+s, s)
    33  			mu.Unlock()
    34  		}
    35  	}
    36  	wg.Add(2)
    37  	go f()
    38  	go f()
    39  	wg.Wait()
    40  }