I have a byte slice in golang that contains binary data. Now in that slice there is a 32 bit
integer that acts as a lock. The slice comes from mapped memory so another process is accessing the same data. Now I need to be able to atomically CompareAndSwap
the contents of the 32 bit
value.
example:
// before:
// slice data [ --- lock value --- ]
// []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ...}
//
// do atomic compare and swap with []byte{0x00, 0x00, 0x00, 0x01} on a given
// index in the original slice.
// after the operation the original slice should contain:
// new data [ --- lock value --- ]
// []byte{0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x01 ...}
I looked at the atomic.CompareAndSwapPointer
method in combination with unsafe.Pointer
but I can't get it to work. Is the above behavior even possible in go? Note that I need the operation to be atomic across other processes. So not only within the current go process.