Thread-safe Boolean

We can also use int32 to represent a Boolean value. We can use the integer 0 as falseand 1 as true, creating a thread-safe Boolean condition:

type cond int32

func (c *cond) Set(v bool) {
a := int32(0)
if v {
a++
}
atomic.StoreInt32((*int32)(c), a)
}

func (c *cond) Value() bool {
return atomic.LoadInt32((*int32)(c)) != 0
}

This will allow us to use the cond type as a thread-safe Boolean value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.82.244