一、第一个例子
package main
import (
"fmt"
"sync"
"time"
)
type person struct {
Name string `json:"Name,omitempty"`
Age int `json:"Age,omitempty"`
}
type testStruct struct {
student *person
}
func (ts *testStruct) testCronFunc() {
for {
fmt.Println(ts.student)
time.Sleep(1 * time.Second)
}
}
func main() {
tempStruct := &testStruct{
student: &person{
Name: "xiaobai",
Age: 90,
},
}
go tempStruct.testCronFunc()
time.Sleep(5 * time.Second)
tempStruct.student = &person{
Name: "xiaoming",
Age: 18,
}
for {
}
}
运行结果:
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
预期应该在第五秒开始输出&{xiaoming 18}
但是输出内容一直没有发生改变,很奇怪的问题,debug也没结论
二、第二个例子
package main
import (
"fmt"
"sync"
"time"
)
type person struct {
Name string `json:"Name,omitempty"`
Age int `json:"Age,omitempty"`
}
type testStruct struct {
student *person
}
func (ts *testStruct) testCronFunc() {
for {
fmt.Println(ts.student)
time.Sleep(1 * time.Second)
}
}
func main() {
tempStruct := &testStruct{
student: &person{
Name: "xiaobai",
Age: 90,
},
}
go tempStruct.testCronFunc()
time.Sleep(5 * time.Second)
tempStruct.student = &person{
Name: "xiaoming",
Age: 18,
}
fmt.Println("sleep is over")
for {
}
}
输出:
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
与例子1比,仅仅加了一句print而已,输出结果就正常了,符合预期了,很奇怪,按理说print不会对结构体造成影响才对
三、分析
查看其汇编代码:
第一个例子:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vMBlNuAx-1673431384066)(/Users/lei/Library/Application Support/typora-user-images/image-20230111165615487.png)]
第二个例子:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kvLoVcf9-1673431384066)(/Users/lei/Library/Application Support/typora-user-images/image-20230111165453588.png)]
从上面不难发现,第一个例子中,
tempStruct.student = &person{
Name: "xiaoming",
Age: 18,
}
这行代码生成的汇编代码,仅仅创建了一个对象,没有将该对象move给tempStruct.student,也没有对new出来的object赋值;
第二个例子中生成的汇编代码,不仅创造了对象(0x00c0),而且还对其进行了赋值操作(0x00d4, 0x00d7),最后把该新object赋给了tempStruct.student(0x00ed),完全是按照我们go代码的逻辑来生成的!
至于为什么出现这种情况,因为代码里有两个线程,并发对tempStruct.student
进行读写操作,且没加锁,因此造成了data race的问题,导致最终由编译器生成的代码逻辑是不确定的,因此需要对并发读写的数据段进行加锁;
三、第三个例子
package main
import (
"fmt"
"sync"
"time"
)
type person struct {
Name string `json:"Name,omitempty"`
Age int `json:"Age,omitempty"`
}
type testStruct struct {
student *person
lock sync.Mutex
}
func (ts *testStruct) testCronFunc() {
for {
ts.lock.Lock()
fmt.Println(ts.student)
ts.lock.Unlock()
time.Sleep(1 * time.Second)
}
}
func main() {
tempStruct := &testStruct{
student: &person{
Name: "xiaobai",
Age: 90,
},
}
go tempStruct.testCronFunc()
time.Sleep(5 * time.Second)
tempStruct.lock.Lock()
tempStruct.student = &person{
Name: "xiaoming",
Age: 18,
}
tempStruct.lock.Unlock()
for {
}
}
输出:
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaobai 90}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
&{xiaoming 18}
可以看到,输出结果已经正常了,并且生成的汇编代码也正常了;
总结
如果有同学进行更多的实现,会发现
(1)即使不加fmt.print
,改为加time.sleep
;
(2)或者把for{}
换成time.sleep
,或者换成select{}
都会出现正常输出的情况,
为什么??因为data race 导致的编译器行为异常,最终导致运行结果不可预测,就像这个问题一样,不知道为什么,且没有逻辑,没有规律,很难排查出问题所在;
参考:
- https://stackoverflow.com/questions/75066407/go-the-print-result-is-not-the-same-when-i-just-add-a-print-statement
- https://www.zhihu.com/question/434964023
- https://go.dev/ref/mem#tmp_2
- https://go.dev/doc/articles/race_detector
- https://gopl-zh.codeyu.com/ch5/ch5-06.html
- http://blog.luoyuanhang.com/2015/07/07/%E5%87%A0%E7%A7%8D%E5%9F%BA%E6%9C%AC%E6%B1%87%E7%BC%96%E6%8C%87%E4%BB%A4%E8%AF%A6%E8%A7%A3/
- https://developer.aliyun.com/article/889778