Implement simple, thread-safe atomic flag type

This commit is contained in:
Kristóf Tóth 2020-08-18 11:06:36 +02:00
parent 202b72879a
commit d4e6a8d727
1 changed files with 27 additions and 0 deletions

27
atomicflag/atomicflag.go Normal file
View 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
}