tsan4.go 634 B

12345678910111213141516171819202122232425262728293031323334
  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. package main
  5. // Check that calls to C.malloc/C.free do not trigger TSAN false
  6. // positive reports.
  7. // #cgo CFLAGS: -fsanitize=thread
  8. // #cgo LDFLAGS: -fsanitize=thread
  9. // #include <stdlib.h>
  10. import "C"
  11. import (
  12. "runtime"
  13. "sync"
  14. )
  15. func main() {
  16. var wg sync.WaitGroup
  17. for i := 0; i < 10; i++ {
  18. wg.Add(1)
  19. go func() {
  20. defer wg.Done()
  21. for i := 0; i < 100; i++ {
  22. p := C.malloc(C.size_t(i * 10))
  23. runtime.Gosched()
  24. C.free(p)
  25. }
  26. }()
  27. }
  28. wg.Wait()
  29. }