指针接收器和非指针接收器的数据竞争差异
Data race difference with Pointer Recevier and Non Pointer Recevier
我在使用 -race
标记进行测试时发现数据争用。更新结构并从结构方法读取值时发生数据竞争。
后来发现把非指针接收者的方法改成指针接收者可以解决数据竞争。但我不明白原因。谁能解释一下原因?
package main
import (
"fmt"
"testing"
)
type TestStruct struct {
display bool
OtherValue int
}
func (t TestStruct) Display() bool {
return t.display
}
func (t *TestStruct) DisplayP() bool {
return t.display
}
func TestNonPointerRecevier(t *testing.T) {
v := &TestStruct{
display: true,
}
go func() {
v.OtherValue = 1
}()
go func() {
fmt.Println(v.Display())
}()
}
func TestPointerRecevier(t *testing.T) {
v := &TestStruct{
display: true,
}
go func() {
v.OtherValue = 1
}()
go func() {
fmt.Println(v.DisplayP())
}()
}
使用指针接收器方法没有错误
go test -race -run ^TestPointerRecevier$
true
PASS
ok _/Users/xxxxx/projects/golang/datarace 0.254s
您在使用非指针接收器方法时出现此错误
go test -race -run ^TestNonPointerRecevier$
==================
WARNING: DATA RACE
Read at 0x00c00001c2c8 by goroutine 9:
_/Users/xxxxx/projects/golang/datarace.TestNonPointerRecevier.func2()
/Users/xxxxx/projects/golang/datarace/main_test.go:30 +0x47
Previous write at 0x00c00001c2c8 by goroutine 8:
_/Users/xxxxx/projects/golang/datarace.TestNonPointerRecevier.func1()
/Users/xxxxx/projects/golang/datarace/main_test.go:27 +0x3e
Goroutine 9 (running) created at:
_/Users/xxxxx/projects/golang/datarace.TestNonPointerRecevier()
/Users/xxxxx/projects/golang/datarace/main_test.go:29 +0xba
testing.tRunner()
/usr/local/Cellar/go/1.15.6/libexec/src/testing/testing.go:1123 +0x202
Goroutine 8 (finished) created at:
_/Users/xxxxx/projects/golang/datarace.TestNonPointerRecevier()
/Users/xxxxx/projects/golang/datarace/main_test.go:26 +0x98
testing.tRunner()
/usr/local/Cellar/go/1.15.6/libexec/src/testing/testing.go:1123 +0x202
==================
true
FAIL
exit status 1
FAIL _/Users/xxxxx/projects/golang/datarace 0.103s
import (
"fmt"
"testing"
)
type TestStruct struct {
display bool
OtherValue int
}
func Display(t TestStruct) bool { // equal func (t TestStruct) Display() bool
return t.display
}
func DisplayP(t *TestStruct) bool { // equal func (t *TestStruct) DisplayP() bool
return t.display
}
func TestNonPointerRecevier(t *testing.T) {
v := &TestStruct{
display: true,
}
go func() {
v.OtherValue = 1 // write
}()
go func() {
fmt.Println(Display(*v)) // *v read value
}()
}
func TestPointerRecevier(t *testing.T) {
v := &TestStruct{
display: true,
}
go func() {
v.OtherValue = 1 // write
}()
go func() {
fmt.Println(DisplayP(v)) // un read, just pass parameter
}()
}
关注data-race错误信息,在调用Display
方法时发生了read
,go会将v(type *TestStruct
)转换为(type TestStruct
).
当方法的接收者值是结构(而不是指向结构的指针)时,将复制完整的结构以按值传递给该方法。
所以调用 v.Display()
隐式读取字段 OtherValue
(当复制结构时),因此出现竞争条件。
另一方面,当使用指针时,只复制指针,同时访问 v.display
和 v.OtherValue
不会触发竞争条件。
我在使用 -race
标记进行测试时发现数据争用。更新结构并从结构方法读取值时发生数据竞争。
后来发现把非指针接收者的方法改成指针接收者可以解决数据竞争。但我不明白原因。谁能解释一下原因?
package main
import (
"fmt"
"testing"
)
type TestStruct struct {
display bool
OtherValue int
}
func (t TestStruct) Display() bool {
return t.display
}
func (t *TestStruct) DisplayP() bool {
return t.display
}
func TestNonPointerRecevier(t *testing.T) {
v := &TestStruct{
display: true,
}
go func() {
v.OtherValue = 1
}()
go func() {
fmt.Println(v.Display())
}()
}
func TestPointerRecevier(t *testing.T) {
v := &TestStruct{
display: true,
}
go func() {
v.OtherValue = 1
}()
go func() {
fmt.Println(v.DisplayP())
}()
}
使用指针接收器方法没有错误
go test -race -run ^TestPointerRecevier$
true
PASS
ok _/Users/xxxxx/projects/golang/datarace 0.254s
您在使用非指针接收器方法时出现此错误
go test -race -run ^TestNonPointerRecevier$
==================
WARNING: DATA RACE
Read at 0x00c00001c2c8 by goroutine 9:
_/Users/xxxxx/projects/golang/datarace.TestNonPointerRecevier.func2()
/Users/xxxxx/projects/golang/datarace/main_test.go:30 +0x47
Previous write at 0x00c00001c2c8 by goroutine 8:
_/Users/xxxxx/projects/golang/datarace.TestNonPointerRecevier.func1()
/Users/xxxxx/projects/golang/datarace/main_test.go:27 +0x3e
Goroutine 9 (running) created at:
_/Users/xxxxx/projects/golang/datarace.TestNonPointerRecevier()
/Users/xxxxx/projects/golang/datarace/main_test.go:29 +0xba
testing.tRunner()
/usr/local/Cellar/go/1.15.6/libexec/src/testing/testing.go:1123 +0x202
Goroutine 8 (finished) created at:
_/Users/xxxxx/projects/golang/datarace.TestNonPointerRecevier()
/Users/xxxxx/projects/golang/datarace/main_test.go:26 +0x98
testing.tRunner()
/usr/local/Cellar/go/1.15.6/libexec/src/testing/testing.go:1123 +0x202
==================
true
FAIL
exit status 1
FAIL _/Users/xxxxx/projects/golang/datarace 0.103s
import (
"fmt"
"testing"
)
type TestStruct struct {
display bool
OtherValue int
}
func Display(t TestStruct) bool { // equal func (t TestStruct) Display() bool
return t.display
}
func DisplayP(t *TestStruct) bool { // equal func (t *TestStruct) DisplayP() bool
return t.display
}
func TestNonPointerRecevier(t *testing.T) {
v := &TestStruct{
display: true,
}
go func() {
v.OtherValue = 1 // write
}()
go func() {
fmt.Println(Display(*v)) // *v read value
}()
}
func TestPointerRecevier(t *testing.T) {
v := &TestStruct{
display: true,
}
go func() {
v.OtherValue = 1 // write
}()
go func() {
fmt.Println(DisplayP(v)) // un read, just pass parameter
}()
}
关注data-race错误信息,在调用Display
方法时发生了read
,go会将v(type *TestStruct
)转换为(type TestStruct
).
当方法的接收者值是结构(而不是指向结构的指针)时,将复制完整的结构以按值传递给该方法。
所以调用 v.Display()
隐式读取字段 OtherValue
(当复制结构时),因此出现竞争条件。
另一方面,当使用指针时,只复制指针,同时访问 v.display
和 v.OtherValue
不会触发竞争条件。