bool.go 397 B

123456789101112131415161718192021
  1. package atomic
  2. import "sync/atomic"
  3. // Boolean is a boolean value, all actions of it is atomic
  4. type Boolean uint32
  5. // Get reads the value atomically
  6. func (b *Boolean) Get() bool {
  7. return atomic.LoadUint32((*uint32)(b)) != 0
  8. }
  9. // Set writes the value atomically
  10. func (b *Boolean) Set(v bool) {
  11. if v {
  12. atomic.StoreUint32((*uint32)(b), 1)
  13. } else {
  14. atomic.StoreUint32((*uint32)(b), 0)
  15. }
  16. }