douheng8629
2014-08-27 05:37
采纳率: 100%
浏览 49
已采纳

原子加载和存储

func resetElectionTimeoutMS(newMin, newMax int) (int, int) {
    oldMin := atomic.LoadInt32(&MinimumElectionTimeoutMS)
    oldMax := atomic.LoadInt32(&maximumElectionTimeoutMS)
    atomic.StoreInt32(&MinimumElectionTimeoutMS, int32(newMin))
    atomic.StoreInt32(&maximumElectionTimeoutMS, int32(newMax))
    return int(oldMin), int(oldMax)
}

I got a go code function like this. The thing I am confused is: why do we need atomic here? What is this preventing from?

Thanks.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douren1891 2014-08-27 06:24
    已采纳

    Atomic functions complete a task in an isolated way where all parts of the task appear to happen instantaneously or don't happen at all.

    In this case, LoadInt32 and StoreInt32 ensure that an integer is stored and retrieved in a way where someone loading won't get a partial store. However, you need both sides to use atomic functions for this to function correctly. The raft example appears incorrect for at least two reasons.

    1. Two atomic functions do not act as one atomic function, so reading the old and setting the new in two lines is a race condition. You may read, then someone else sets, then you set and you are returning false information for the previous value before you set it.

    2. Not everyone accessing MinimumElectionTimeoutMS is using atomic operations. This means that the use of atomics in this function is effectively useless.

    How would this be fixed?

    func resetElectionTimeoutMS(newMin, newMax int) (int, int) {
        oldMin := atomic.SwapInt32(&MinimumElectionTimeoutMS, int32(newMin))
        oldMax := atomic.SwapInt32(&maximumElectionTimeoutMS, int32(newMax))
        return int(oldMin), int(oldMax)
    }
    

    This would ensure that oldMin is the minimum that existed before the swap. However, the entire function is still not atomic as the final outcome could be an oldMin and oldMax pair that was never called with resetElectionTimeoutMS. For that... just use locks.

    Each function would also need to be changed to do an atomic load:

    func minimumElectionTimeout() time.Duration {
        min := atomic.LoadInt32(&MinimumElectionTimeoutMS)
        return time.Duration(min) * time.Millisecond
    }
    

    I recommend you carefully consider the quote VonC mentioned from the golang atomic documentation:

    These functions require great care to be used correctly. Except for special, low-level applications, synchronization is better done with channels or the facilities of the sync package.

    If you want to understand atomic operations, I recommend you start with http://preshing.com/20130618/atomic-vs-non-atomic-operations/. That goes over the load and store operations used in your example. However, there are other uses for atomics. The go atomic package overview goes over some cool stuff like atomic swapping (the example I gave), compare and swap (known as CAS), and Adding.

    A funny quote from the link I gave you:

    it’s well-known that on x86, a 32-bit mov instruction is atomic if the memory operand is naturally aligned, but non-atomic otherwise. In other words, atomicity is only guaranteed when the 32-bit integer is located at an address which is an exact multiple of 4.

    In other words, on common systems today, the atomic functions used in your example are effectively no-ops. They are already atomic! (They are not guaranteed though, if you need it to be atomic, it is better to specify it explicitly)

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanoucuo7045 2014-08-27 06:10

    Considering that the package atomic provides low-level atomic memory primitives useful for implementing synchronization algorithms, I suppose it was intended to be used as:

    • MinimumElectionTimeoutMS isn't modified while being stored in oldMin
    • MinimumElectionTimeoutMS isn't modified while being set to a new value newMin.

    But, the package does come with the warning:

    These functions require great care to be used correctly.
    Except for special, low-level applications, synchronization is better done with channels or the facilities of the sync package.
    Share memory by communicating; don't communicate by sharing memory.

    In this case (server.go from the Raft distributed consensus protocol), synchronizing directly on the variable might be deemed faster than putting a Mutex on the all function.

    Except, as Stephen Weinberg's answer illustrate (upvoted), this isn't how you use atomic. It only makes sure that oldMin is accurate while doing the swap.

    See another example at "Is the two atomic style code in sync/atomic.once.go necessary?", in relation with the "memory model".


    OneOfOne mentions in the comments using atomic CAS as a spinlock (very fast locking):

    BenchmarkSpinL-8            2000            708494 ns/op           32315 B/op       2001 allocs/op
    BenchmarkMutex-8            1000           1225260 ns/op           78027 B/op       2259 allocs/op
    

    See:

    评论
    解决 无用
    打赏 举报