-
-
Save johnteee/f9a5539dd56a22d84a555eb988870f2d to your computer and use it in GitHub Desktop.
Atomic boolean for golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Atomic boolean for golang | |
| A process-atomic boolean that can be used for signaling between goroutines. | |
| Default value = false. (nil structure) | |
| */ | |
| package main | |
| import "sync/atomic" | |
| type TAtomBool struct {flag int32} | |
| func (b *TAtomBool) Set(value bool) { | |
| var i int32 = 0 | |
| if value {i = 1} | |
| atomic.StoreInt32(&(b.flag), int32(i)) | |
| } | |
| func (b *TAtomBool) Get() bool { | |
| if atomic.LoadInt32(&(b.flag)) != 0 {return true} | |
| return false | |
| } | |
| //Test code | |
| func main() { | |
| b1 := new(TAtomBool) | |
| println(b1.Get()) | |
| b1.Set(true) | |
| println(b1.Get()) | |
| b2 := &TAtomBool{1} | |
| println(b2.Get()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment