前辈们,帮我看个关于并发安全的问题

zhengkeyu · · 330 次点击
fenglangjuxu
出售域名 http://letsgo.xin
```golang go run --race race_wcd.go 100 ================== WARNING: DATA RACE Write at 0x00c00012a018 by goroutine 7: main.main.func1() /Users/wcd/code/go/src/tools/race_wcd.go:23 +0xac Previous read at 0x00c00012a018 by main goroutine: main.main() /Users/wcd/code/go/src/tools/race_wcd.go:29 +0x179 Goroutine 7 (running) created at: main.main() /Users/wcd/code/go/src/tools/race_wcd.go:20 +0x15e ================== ================== WARNING: DATA RACE Read at 0x00c00012e010 by main goroutine: main.main() /Users/wcd/code/go/src/tools/race_wcd.go:30 +0x1a5 Previous write at 0x00c00012e010 by goroutine 7: main.main.func1() /Users/wcd/code/go/src/tools/race_wcd.go:23 +0x91 Goroutine 7 (running) created at: main.main() /Users/wcd/code/go/src/tools/race_wcd.go:20 +0x15e ================== 7266 6979 ```
#2
更多评论
* 非并发安全,在并发读写时,会存在读到没有写完的对象,Golang 不保证任何单独的操作是原子性的,除非使用 atomic 包里提供的原语或加锁。 * 也可以看下这篇文章: https://studygolang.com/articles/29922 * go run -race main.go , 开启竞态检测,会有WARNING提示
#1