Implement simple, thread-safe atomic flag type
This commit is contained in:
parent
202b72879a
commit
d4e6a8d727
27
atomicflag/atomicflag.go
Normal file
27
atomicflag/atomicflag.go
Normal file
@ -0,0 +1,27 @@
|
||||
package atomicflag
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
||||
type atomicflag struct {
|
||||
value bool
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func New(value bool) *atomicflag {
|
||||
return &atomicflag{value: value}
|
||||
}
|
||||
|
||||
func (f* atomicflag) Get() bool {
|
||||
f.mutex.RLock()
|
||||
defer f.mutex.RUnlock()
|
||||
return f.value
|
||||
}
|
||||
|
||||
func (f* atomicflag) Set(value bool) {
|
||||
f.mutex.Lock()
|
||||
defer f.mutex.Unlock()
|
||||
f.value = value
|
||||
}
|
Loading…
Reference in New Issue
Block a user