Lock()
unLock()
import (
"sync"
"time"
)
type atomic struct {
value int
lock sync.Mutex
}
func (a *atomic) increment() {
a.lock.Lock()
defer a.lock.Unlock()
a.value++
}
func (a *atomic) get() int {
a.lock.Lock()
defer a.lock.Unlock()
return a.value
}`
func main() {
var a atomic
a.increment()
go func() {
a.increment()
}()
time.Sleep(time.Second)
println(a.get())
}
lock和d efer unlock在该函数体内生效
fmt.Println("safe inrement")
func() {
a.lock.Lock()
defer a.lock.Unlock()
a.value++
}()
}```