I need a function to atomically add float32 values in Go. This is what came up with based on some C code I found:
package atomic
import (
"sync/atomic"
"unsafe"
"math"
)
func AddFloat32(addr *float32, delta float32) (new float32) {
unsafeAddr := (*uint32)(unsafe.Pointer(addr))
for {
oldValue := math.Float32bits(*addr)
new = *addr + delta
newValue := math.Float32bits(new)
if atomic.CompareAndSwapUint32(unsafeAddr, oldValue, newValue) {
return
}
}
}
Should it work (i.e really be atomic)? Is there a better/faster way to do it in Go?