doudouwen2763 2016-12-23 15:49
浏览 518

何时使用atomic.LoadPointer

What is the difference between using atomic.StorePointer / LoadPointer:

data := "abc"
atomic.StorePointer(&p, unsafe.Pointer(&data))
fmt.Printf("value is %s
", *(*string)(atomic.LoadPointer(&p)))

And just using using the pointer normally?

data := "abc"
p = unsafe.Pointer(&data)
fmt.Printf("value is %s
", *(*string)(p))

What could go wrong if I decide to just read from a pointer like in the second example, instead of using LoadPointer? I can guess there might be some kind of race, but practically, what could actually go wrong?

Some examples:

  • 写回答

1条回答 默认 最新

  • dpwqicw157673 2016-12-23 16:10
    关注

    As long as you only have one goroutine accessing the value, nothing will go wrong. As soon as you have several goroutines, you need atomic access in order to read/write the latest value (not the possibly stale value that is in your CPU cache).

    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题