0
点赞
收藏
分享

微信扫一扫

Go 32位程序原子指令 panic

江南北 2022-02-11 阅读 95
type MySt struct {
	a uint8
	b uint64
}

func main() {
	var my MySt
	my.a = 0
	my.b = 16
	atomic.AddUint64(&my.b, 1)
}

上述在64位操作系统上工作正常。

当我们设置编译参数编译32位程序

SET GOARCH=386
go build

再次运行该程序就会发生Painc

sync/atomic.AddUint64(0x10d7a004, 0x1, 0x0, 0x10d2e280, 0x10d78030)
        D:/go1.10.windows-amd64/go/src/sync/atomic/asm_386.s:112 +0xc
main.main()
        D:/go1.10.windows-amd64/gopath/src/git.hzauth.com/gmsslvpn/unisign-tlcp-pxy/proxy/test/main.go:14 +0x50

32位系统下,变量最大存储也就是uint32也就是4个字节

当我们在程序中使用uint64时,如果没有对该变量的内存对其那么就会导致GO原子指令panic

参考文献

[1]. 开发者头条 . Go 夜读 . Go 在 32 位系统中使用 64 位原子操作的坑 . https://toutiao.io/posts/jagmqm/preview
[2]. golang . https://golang.google.cn/pkg/sync/atomic/#pkg-note-BUG

举报

相关推荐

0 条评论