go语言踩坑:data race导致的输出结果与预期结果不一致
创始人
2024-05-10 14:04:07
0

一、第一个例子

package mainimport ("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 mainimport ("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 mainimport ("fmt""sync""time"
)type person struct {Name string `json:"Name,omitempty"`Age  int    `json:"Age,omitempty"`
}type testStruct struct {student *personlock    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 导致的编译器行为异常,最终导致运行结果不可预测,就像这个问题一样,不知道为什么,且没有逻辑,没有规律,很难排查出问题所在;

参考:

  1. https://stackoverflow.com/questions/75066407/go-the-print-result-is-not-the-same-when-i-just-add-a-print-statement
  2. https://www.zhihu.com/question/434964023
  3. https://go.dev/ref/mem#tmp_2
  4. https://go.dev/doc/articles/race_detector
  5. https://gopl-zh.codeyu.com/ch5/ch5-06.html
  6. 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/
  7. https://developer.aliyun.com/article/889778

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...