github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/misc/cgo/testsanitizers/tsan.go (about)

     1  // Copyright 2015 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  // This program produced false race reports when run under the C/C++
     8  // ThreadSanitizer, as it did not understand the synchronization in
     9  // the Go code.
    10  
    11  /*
    12  #cgo CFLAGS: -fsanitize=thread
    13  #cgo LDFLAGS: -fsanitize=thread
    14  
    15  int val;
    16  
    17  int getVal() {
    18  	return val;
    19  }
    20  
    21  void setVal(int i) {
    22  	val = i;
    23  }
    24  */
    25  import "C"
    26  
    27  import (
    28  	"runtime"
    29  )
    30  
    31  func main() {
    32  	runtime.LockOSThread()
    33  	C.setVal(1)
    34  	c := make(chan bool)
    35  	go func() {
    36  		runtime.LockOSThread()
    37  		C.setVal(2)
    38  		c <- true
    39  	}()
    40  	<-c
    41  	if v := C.getVal(); v != 2 {
    42  		panic(v)
    43  	}
    44  }