go语言踩坑: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
}

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 导致的编译器行为异常,最终导致运行结果不可预测,就像这个问题一样,不知道为什么,且没有逻辑,没有规律,很难排查出问题所在;

参考:

  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

THMAIL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值