dsjws44266
2018-01-25 15:07 阅读 123
已采纳

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)
...

etc.

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongwuqi4243 dongwuqi4243 2018-01-25 15:39

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

    点赞 评论 复制链接分享

相关推荐