6

How to detect data races

 3 years ago
source link: https://yourbasic.org/golang/detect-data-races/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to detect data races

yourbasic.org/golang
sherlock-with-looking-glass.png

Data races can happen easily and are hard to debug. Luckily, the Go runtime is often able to help.

Use -race to enable the built-in data race detector.

$ go test -race [packages]
$ go run -race [packages]

Example

Here’s a program with a data race:

package main
import "fmt"

func main() {
    i := 0
    go func() {
        i++ // write
    }()
    fmt.Println(i) // concurrent read
}

Running this program with the -race options tells us that there’s a race between the write at line 7 and the read at line 9:

$ go run -race main.go
0
==================
WARNING: DATA RACE
Write by goroutine 6:
  main.main.func1()
      /tmp/main.go:7 +0x44

Previous read by main goroutine:
  main.main()
      /tmp/main.go:9 +0x7e

Goroutine 6 (running) created at:
  main.main()
      /tmp/main.go:8 +0x70
==================
Found 1 data race(s)
exit status 66

Details

The data race detector does not perform any static analysis. It checks the memory access in runtime and only for the code paths that are actually executed.

It runs on darwin/amd64, freebsd/amd64, linux/amd64 and windows/amd64.

The overhead varies, but typically there’s a 5-10x increase in memory usage, and 2-20x increase in execution time.

Share:             


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK