In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The CompareAndSwapUint32() function in Go language is used to perform the compare and swap operation for an uint32 value. This function is defined under the atomic package. Here, you need to import “sync/atomic” package in order to use these functions.
func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)
Here, addr indicates address, old indicates uint32 value that is old one, and new is the uint32 new value that will swap itself from the old value.
Note: (*uint32) is the pointer to a uint32 value. And uint32 is integer type of bit size 32. Moreover, int32 contains the set of all unsigned 32-bit integers ranging from 0 to 4294967295.
Return Value: It returns true if swapping is accomplished else it returns false.
true The value of i is: 67576
Swapped_value: 7687 , old_value: 54325 false The value of i is: 7687
Here, the swapped value obtained from the swapping operation must be the old value that’s why false is returned.