golang--- 32位系统下atomic.AddUint64导致程序崩溃及解决办法
来源:互联网 发布:6sigma软件 编辑:程序博客网 时间:2024/06/05 16:07
阅读(7) | 评论(0)
0人收藏此文章,
赞0
package mainimport ("sync/atomic""unsafe")type a struct {x unsafe.Pointery uint64}func main() {p := new(a)atomic.AddUint64(&p.y, 1)}
在32位计算机上运行改程序,会出现错误:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x4198bc]
goroutine 1 [running]:
runtime.panic(0x41c740, 0x445e4f)
C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist550409343/go/src/pkg/runtime/panic.c:266 +0xa6
sync/atomic.AddUint64(0x114434ac, 0×1, 0×0, 0x4107e3, 0x397fcc)
C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist550409343/go/src/pkg/sync/atomic/asm_386.s:118 +0xc
main.main()
E:/Work/GoLang/src/demo/goBug/atomic/main.go:17 +0x4d
查了资料具体原因如下:
https://code.google.com/p/go/issues/detail?id=5278
On x86-32, the 64-bit functions use instructions unavailable before the
Pentium MMX. On both ARM and x86-32, it is the caller’s responsibility to
arrange for 64-bit alignment of 64-bit words accessed atomically. The
first word in a global variable or in an allocated struct or slice can be
relied upon to be 64-bit aligned.
其大意是:
64位原子操作的调用者必须确保指针的地址是对齐到8字节的边界
的办法有两种:
1,修改uint64字段在struct 的位置确保字段地址出现在8字节的边界;
2,修改使用sync.RWMutex来实现互斥,如下
mutex.Lock()uint64 += 1mutex.Unlock()
建议使用办法2来彻底解决问题
声明:OSCHINA 博客文章版权属于作者,受法律保护。未经作者同意不得转载。
- golang--- 32位系统下atomic.AddUint64导致程序崩溃及解决办法
- 64位系统下IIS无法运行32位程序的解决办法
- C#程序无法在64位系统下的解决办法
- Project Server 2010 SharePoint_AdminContent 错误导致系统崩溃的解决办法
- WebView导致程序崩溃!!!
- 多线程导致程序崩溃
- printk导致系统崩溃
- 诺顿误杀导致系统崩溃--起因及对策
- 在32位系统下编译64位程序
- 64位系统下用32位程序
- 在32位系统下编译64位程序
- dword与dword_ptr类型的区别及64位系统下调用系统函数直接崩溃的问题
- QTabWidget bug导致程序崩溃
- 数组越界导致程序崩溃
- pthread_create导致的程序崩溃
- JVM崩溃及解决办法
- 64位win7下使用C_API编写mysql程序的错误及解决办法记录
- win8-64位系统下,VC++2010中设置FFTW时遇到的若干问题及解决办法
- CLassLoader载入类的原理
- check out your most used linux commands
- NSString转NSNumber:
- hdu4930Fighting the Landlords(直接枚举)
- 暑期个人赛--第九场--C(字符串 朴素查找)
- golang--- 32位系统下atomic.AddUint64导致程序崩溃及解决办法
- poj3252Round Numbers
- UIViewController的切换方式
- 排序算法
- gdfsgdskgjkdlf
- 什么是java序列化,如何实现java序列化?
- sqlserver中newid()和newsequentialid()的区别
- 使用Code Snippet在Xcode中添加代码段
- 每天一个linux命令:wget命令