-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.go
81 lines (71 loc) · 1.44 KB
/
sync.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Copyright(C)Michał Łowicki 2018. The original code can be found
https://play.golang.org/p/xoiqW0RQQE9. The discussion of the code
can be found https://medium.com/golangspec/sync-rwmutex-ca6c6c3208a0.
*/
package main
import (
"fmt"
"math/rand"
"strings"
"sync"
"time"
)
// init sets seed for random number generation
func init() {
rand.Seed(time.Now().Unix())
}
// sleep mimics a task execution with random duration ~ (0 - 1000 milliseconds)
func sleep() {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
}
// reader mimics a reading task
func reader(c chan int, m *sync.RWMutex, wg *sync.WaitGroup) {
sleep()
m.RLock()
c <- 1
sleep()
c <- -1
m.RUnlock()
wg.Done()
}
// writer mimics a writing task
func writer(c chan int, m *sync.RWMutex, wg *sync.WaitGroup) {
sleep()
m.Lock()
c <- 1
sleep()
c <- -1
m.Unlock()
wg.Done()
}
func main() {
var m sync.RWMutex
var rs, ws int
// make two channels for concurrent tracking readers and writers
rsCh := make(chan int)
wsCh := make(chan int)
// print current readers and writers
go func() {
for {
select {
case n := <-rsCh:
rs += n
case n := <-wsCh:
ws += n
}
fmt.Printf("%s%s...", strings.Repeat("R", rs), strings.Repeat("W", ws))
}
}()
// perform reading and writing tasks
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go reader(rsCh, &m, &wg)
}
for i := 0; i < 3; i++ {
wg.Add(1)
go writer(wsCh, &m, &wg)
}
wg.Wait()
}