正如标题所说。基本上我想知道的是atomic.StoreInt32在写入时也会锁定读取操作吗?另一个相关问题:atomic.StoreUint64(&procRate,procCount)是否等同于atomic.StoreUint64(&procRate,atomic.LoadUint64(&procCount))?提前致谢。 最佳答案 是的,当您同时加载和存储相同的值时,您需要使用原子操作。竞争检测器应该就此向您发出警告。关于第二个问题,如果procCount值也被并发使用,那么还是需要使用原子操作加载。这两个不是等价的:atom
此代码已简化并描述了我的问题。atomic.StoreInt32似乎不起作用,但我不确定为什么。packagemainimport("fmt""sync/atomic")typeslavestruct{failedint32}funcNewSlave()slave{returnslave{0}}func(workerslave)Fail(){atomic.StoreInt32(&worker.failed,1)//Here'stheproblem.}func(workerslave)IsFailed()bool{failed:=atomic.LoadInt32(&worker.fail
谁能展示需要使用此类原子操作的示例。我不明白之间的区别import"sync/atomic"...varsharedAint64varsharedB*int64...//concurentcodetmpVarA:=sharedAtmpVarB:=*sharedB//andtmpVarA:=atomic.LoadInt64(&sharedA)tmpVarB:=atomic.LoadInt64(sharedB) 最佳答案 它根本没有记录在包中,但通常原子加载和正常值的存储不是为了原子性,因为CPU操作已经是原子的,而是为了排序。如果您使
谁能展示需要使用此类原子操作的示例。我不明白之间的区别import"sync/atomic"...varsharedAint64varsharedB*int64...//concurentcodetmpVarA:=sharedAtmpVarB:=*sharedB//andtmpVarA:=atomic.LoadInt64(&sharedA)tmpVarB:=atomic.LoadInt64(sharedB) 最佳答案 它根本没有记录在包中,但通常原子加载和正常值的存储不是为了原子性,因为CPU操作已经是原子的,而是为了排序。如果您使