-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path37_mutexes.go
More file actions
75 lines (65 loc) · 1.75 KB
/
37_mutexes.go
File metadata and controls
75 lines (65 loc) · 1.75 KB
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
package gobyexample
import (
"fmt"
"math/rand"
"sync"
"sync/atomic"
"time"
)
// MutexesDemo - demonstrates using mutexes to safely access data across
// mutiple goroutines
func MutexesDemo() {
// manage our state in a map
var state = make(map[int]int)
// this mutex will synchronize access to `state`
var mutex = &sync.Mutex{}
// keep track of how many read and write operations we do
var readOps uint64
var writeOps uint64
// start 100 goroutines to execute repeated reads against the
// state, once per millisecond in each goroutine
for r := 0; r < 100; r++ {
go func() {
// for each read, we pick a key to access, Lock() the mutex
// to ensure exclusive access to the `state`, read the value
// at the chosen key, Unlock() the mutex, and increment the
// `readOps` count
total := 0
for {
key := rand.Intn(5)
mutex.Lock()
total += state[key]
mutex.Unlock()
atomic.AddUint64(&readOps, 1)
// wait a bit between reads
time.Sleep(time.Millisecond)
}
}()
}
// we also start 10 goroutines to simulate writes, using the same
// pattern we did for reads
for w := 0; w < 10; w++ {
go func() {
for {
key := rand.Intn(5)
val := rand.Intn(100)
mutex.Lock()
state[key] = val
mutex.Unlock()
atomic.AddUint64(&writeOps, 1)
time.Sleep(time.Millisecond)
}
}()
}
// let the 10 goroutines work on the `state` and `mutex` for a second
time.Sleep(time.Second)
// take and report final operation counts
readOpsFinal := atomic.LoadUint64(&readOps)
fmt.Println("readOps:", readOpsFinal)
writeOpsFinal := atomic.LoadUint64(&writeOps)
fmt.Println("writeOps:", writeOpsFinal)
// with a final lock of `state`, show how it ended up
mutex.Lock()
fmt.Println("state:", state)
mutex.Unlock()
}