2018-01-25 15:07
浏览 176

Golang如何像c(gcc buildin)一样实现__sync_fetch_and_add?

In go's sync/atomic library, it seems there is no function like __sync_fetch_and_add in c(gcc buildin), it has

func AddInt32(addr *int32, delta int32) (new int32)
func AddInt64(addr *int64, delta int64) (new int64)
func AddUint32(addr *uint32, delta uint32) (new uint32)
func AddUint64(addr *uint64, delta uint64) (new uint64)
func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)


It seems we only can implement it by CompareAndSwapxxx + while-loop, is it?

图片转代码服务由CSDN问答提供 功能建议

在go的同步/原子库中,似乎在c(gcc buildin)中没有像__sync_fetch_and_add这样的函数,它具有

  func AddInt32(addr * int32,delta int32)(新int32)
func AddInt64(addr * int64,delta int64)(新int64)
func AddUint32(addr * uint32  ,delta uint32)(新uint32)
func AddUint64(addr * uint64,delta uint64)(新uint64)
func AddUintptr(addr * uintptr,delta uintptr)(新uintptr)
func CompareAndSwapInt32(addr * int32,旧,新int  )(交换布尔)
func CompareAndSwapInt64(addr * int64,old,new int64)(交换布尔)


似乎我们只能通过 CompareAndSwapxxx + while-loop 实现它,是吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwuqi4243 2018-01-25 15:39

    Subtract delta from the AddXXX return value to get the original value.

    打赏 评论

相关推荐 更多相似问题