As in the title, are read and write operations regarding uint8, atomic? Logically it must be a single cpu instruction obviously to read and write for a 8 bit variable. But in any case, two cores could simultaneously read and write from the memory, is it possible to create a stale data this way?
2条回答 默认 最新
- dsfs1233 2016-03-21 14:18关注
There's no guarantee that the access on native types are on any platform atomic. This is why there is
sync/atomic
. See also the advice in the memory model documentation.Example for generic way of atomically setting a value (Play)
var ax atomic.Value // may be globally accessible x := uint8(5) // set atomically ax.Store(x) x = ax.Load().(uint8)
Probably more efficient solution for
uint8
(Play):var ax int64 // may be globally accessible x := uint8(5) atomic.StoreInt64(&ax, 10) x = uint8(atomic.LoadInt64(&ax)) fmt.Printf("%T %v ", x, x)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥50 burgers方程求逆
- ¥15 matlab最小二乘法拟合非线性曲线提问
- ¥15 电脑锁屏时vba运行SAP,excel数据不能复制到SAP
- ¥50 74LS系列 74LS00 74LS04设计一个RS485电路(关键词-差分)
- ¥30 各位help写一下代码
- ¥15 在运行SDEdit模型下载不了
- ¥15 求51控制l298n驱动的小车中超声波避障怎么写
- ¥15 电脑连上WIFI却用不了
- ¥30 MATLAB在RLC电路的固有响应和阶跃响应GUI仿真报告
- ¥15 hyper-v出现的问题