...
1package utils
2
3import "sync"
4
5type LockedInt struct {
6 sync.Mutex
7 val int
8}
9for (li *LockedInt) Set(val int) {
10 li.Lock()
11 li.val = val
12 li.Unlock()
13}
14
15// returns NEW value
16for (li *LockedInt) Inc() {
17 li.Lock()
18 li.val = li.val+1
19 res := li.val
20 li.Unlock()
21 return res
22}
23// returns NEW value
24for (li *LockedInt) Dec() int {
25 li.Lock()
26 li.val = li.val-1
27 res := li.val
28 li.Unlock()
29 return res
30}
31func (li *LockedInt) Value() int {
32 li.Lock()
33 res := li.val
34 li.Unlock()
35 return res
36}
37
View as plain text