这代码有 race 啊

#1 · closed · 7 comments

View on GitHub ↗

choleraehyq

不知道为啥要在汇编代码前面加 `// build !race` 去了之后写了个俩消费者俩生产者的 demo,然后 `go run -race demo.go` 跑了一下就 race 了。 看到你给 go 提的 xor 的 issue 追过来的,想顺便问一下你提到的 intel 汇编的那个指令编程优化手册是在哪找到的,能否发个链接? demo.go: ``` package main import ( "sync" "github.com/templexxx/fqueue" ) func main() { q, err := fqueue.New(16) if err != nil { panic(err) } var w sync.WaitGroup w.Add(4) go func() { defer w.Done() for i := 0; i < 10000; i++ { q.Push(i) } }() go func() { defer w.Done() for i := 10000; i < 20000; i++ { q.Push(i) } }() go func() { defer w.Done() for i := 0; i < 10000; i++ { q.Get() } }() go func() { defer w.Done() for i := 10000; i < 20000; i++ { q.Get() } }() w.Wait() } ``` ``` ================== WARNING: DATA RACE Read at 0x00c42007c028 by goroutine 7: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:111 +0x15e main.main.func3() /home/cholerae/test/gotest/testfqueue.go:30 +0x6b Previous write at 0x00c42007c028 by goroutine 5: github.com/templexxx/fqueue.(*queue).Push() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:77 +0x1b8 main.main.func1() /home/cholerae/test/gotest/testfqueue.go:18 +0x9e Goroutine 7 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:27 +0x1c0 Goroutine 5 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:15 +0x168 ================== ================== WARNING: DATA RACE Read at 0x00c42007c040 by goroutine 8: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:111 +0x15e main.main.func4() /home/cholerae/test/gotest/testfqueue.go:36 +0x6e Previous write at 0x00c42007c040 by goroutine 5: github.com/templexxx/fqueue.(*queue).Push() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:77 +0x1b8 main.main.func1() /home/cholerae/test/gotest/testfqueue.go:18 +0x9e Goroutine 8 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:33 +0x1ec Goroutine 5 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:15 +0x168 ================== ================== WARNING: DATA RACE Read at 0x00c42007c030 by goroutine 8: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:112 +0x196 main.main.func4() /home/cholerae/test/gotest/testfqueue.go:36 +0x6e Previous write at 0x00c42007c030 by goroutine 5: github.com/templexxx/fqueue.(*queue).Push() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:76 +0x17e main.main.func1() /home/cholerae/test/gotest/testfqueue.go:18 +0x9e Goroutine 8 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:33 +0x1ec Goroutine 5 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:15 +0x168 ================== ================== WARNING: DATA RACE Read at 0x00c42007c018 by goroutine 7: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:112 +0x196 main.main.func3() /home/cholerae/test/gotest/testfqueue.go:30 +0x6b Previous write at 0x00c42007c018 by goroutine 5: github.com/templexxx/fqueue.(*queue).Push() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:76 +0x17e main.main.func1() /home/cholerae/test/gotest/testfqueue.go:18 +0x9e Goroutine 7 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:27 +0x1c0 Goroutine 5 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:15 +0x168 ================== ================== WARNING: DATA RACE Read at 0x00c42007d1d0 by goroutine 7: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:112 +0x196 main.main.func3() /home/cholerae/test/gotest/testfqueue.go:30 +0x6b Previous write at 0x00c42007d1d0 by goroutine 5: [failed to restore the stack] Goroutine 7 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:27 +0x1c0 Goroutine 5 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:15 +0x168 ================== ================== WARNING: DATA RACE Read at 0x00c42007d1f8 by goroutine 8: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:111 +0x15e main.main.func4() /home/cholerae/test/gotest/testfqueue.go:36 +0x6e Previous write at 0x00c42007d1f8 by goroutine 5: [failed to restore the stack] Goroutine 8 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:33 +0x1ec Goroutine 5 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:15 +0x168 ================== ================== WARNING: DATA RACE Read at 0x00c42007d210 by goroutine 7: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:111 +0x15e main.main.func3() /home/cholerae/test/gotest/testfqueue.go:30 +0x6b Previous write at 0x00c42007d210 by goroutine 5: [failed to restore the stack] Goroutine 7 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:27 +0x1c0 Goroutine 5 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:15 +0x168 ================== ================== WARNING: DATA RACE Read at 0x00c42007d1e8 by goroutine 8: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:112 +0x196 main.main.func4() /home/cholerae/test/gotest/testfqueue.go:36 +0x6e Previous write at 0x00c42007d1e8 by goroutine 5: [failed to restore the stack] Goroutine 8 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:33 +0x1ec Goroutine 5 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:15 +0x168 ================== ================== WARNING: DATA RACE Read at 0x00c4200b9738 by goroutine 7: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:111 +0x15e main.main.func3() /home/cholerae/test/gotest/testfqueue.go:30 +0x6b Previous write at 0x00c4200b9738 by goroutine 6: github.com/templexxx/fqueue.(*queue).Push() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:77 +0x1b8 main.main.func2() /home/cholerae/test/gotest/testfqueue.go:24 +0xa1 Goroutine 7 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:27 +0x1c0 Goroutine 6 (finished) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:21 +0x194 ================== ================== WARNING: DATA RACE Read at 0x00c4200b9750 by goroutine 8: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:111 +0x15e main.main.func4() /home/cholerae/test/gotest/testfqueue.go:36 +0x6e Previous write at 0x00c4200b9750 by goroutine 6: github.com/templexxx/fqueue.(*queue).Push() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:77 +0x1b8 main.main.func2() /home/cholerae/test/gotest/testfqueue.go:24 +0xa1 Goroutine 8 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:33 +0x1ec Goroutine 6 (finished) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:21 +0x194 ================== ================== WARNING: DATA RACE Read at 0x00c4200b9728 by goroutine 7: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:112 +0x196 main.main.func3() /home/cholerae/test/gotest/testfqueue.go:30 +0x6b Previous write at 0x00c4200b9728 by goroutine 6: github.com/templexxx/fqueue.(*queue).Push() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:76 +0x17e main.main.func2() /home/cholerae/test/gotest/testfqueue.go:24 +0xa1 Goroutine 7 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:27 +0x1c0 Goroutine 6 (finished) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:21 +0x194 ================== ================== WARNING: DATA RACE Read at 0x00c4200b9740 by goroutine 8: github.com/templexxx/fqueue.(*queue).Get() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:112 +0x196 main.main.func4() /home/cholerae/test/gotest/testfqueue.go:36 +0x6e Previous write at 0x00c4200b9740 by goroutine 6: github.com/templexxx/fqueue.(*queue).Push() /home/cholerae/gopath/src/github.com/templexxx/fqueue/fqueue.go:76 +0x17e main.main.func2() /home/cholerae/test/gotest/testfqueue.go:24 +0xa1 Goroutine 8 (running) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:33 +0x1ec Goroutine 6 (finished) created at: main.main() /home/cholerae/test/gotest/testfqueue.go:21 +0x194 ================== Found 12 data race(s) exit status 66 ```

Comments

templexxx

@choleraehyq 额 这代码就是写着玩的。。。

templexxx

@choleraehyq 你说的是 intel 手册? 我在哪里提到的? 有很多卷啊

templexxx

@choleraehyq 这本来就是共享内存 要竞争的啊

choleraehyq

@templexxx 在 go 中,带 race 的代码等同于未定义行为,讨论这种代码的正确性是没有意义的。 手册就是你在[当你们在谈论锁的时候,我听不懂你们在谈论什么](http://www.templex.xyz/blog/101/lockfree.html) 这篇博客里提到的Intel 的指令、编程与优化三大指南还有手册之类的。

choleraehyq

无锁不代表连原子操作的保护都不用就直接操作。。。我还没见过靠谱的 go 写的无锁队列。

templexxx

@choleraehyq 原子操作的保护?什么意思,没听过啊。。。

templexxx

@choleraehyq 你可以搜一下 64-ia-32-architectures-optimization-manual software-develop-manul-combined 包含 基础篇 指令列表 优化等内容