add atomic bool

This commit is contained in:
siddontang 2014-11-21 09:53:23 +08:00
parent ecf49fc073
commit cdaddb8a0e
2 changed files with 31 additions and 0 deletions

View File

@ -130,3 +130,17 @@ func (s *AtomicString) CompareAndSwap(oldval, newval string) (swqpped bool) {
}
return false
}
type AtomicBool int32
func (b *AtomicBool) Set(v bool) {
if v {
atomic.StoreInt32((*int32)(b), 1)
} else {
atomic.StoreInt32((*int32)(b), 0)
}
}
func (b *AtomicBool) Get() bool {
return atomic.LoadInt32((*int32)(i)) == 1
}

View File

@ -29,4 +29,21 @@ func TestAtomicString(t *testing.T) {
if s.Get() != "c" {
t.Errorf("want c, got %s", s.Get())
}
var b AtomicBool
if b.Get() != false {
t.Fatal("must false")
}
b.Set(true)
if b.Get() != true {
t.Fatal("must true")
}
b.Set(false)
if b.Get() != false {
t.Fatal("must false")
}
}