-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrwmutex.go
63 lines (50 loc) · 881 Bytes
/
rwmutex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"sync"
"time"
)
type TimeSystem struct {
updates int
currentTime time.Time
lock sync.RWMutex
}
var Ts TimeSystem
func update(et chan bool) {
Ts.lock.Lock()
defer Ts.lock.Unlock()
Ts.currentTime = time.Now()
Ts.updates++
if Ts.updates == 2 {
et <- false
}
}
func main() {
wg := new(sync.WaitGroup)
Ts.updates = 0
Ts.currentTime = time.Now()
timer := time.NewTicker(1 * time.Second)
writeTimer := time.NewTicker(10 * time.Second)
endTimer := make(chan bool)
breakPoint := false
wg.Add(1)
for {
if breakPoint {
break
}
select {
case <-timer.C:
fmt.Println(Ts.updates, Ts.currentTime.String())
case <-writeTimer.C:
update(endTimer)
case <-endTimer:
timer.Stop()
close(endTimer)
wg.Done()
breakPoint = true
// return
}
}
wg.Wait()
fmt.Println(Ts.currentTime.String())
}