forked from hoanhan101/ultimate-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_race_3.go
74 lines (60 loc) · 2.11 KB
/
data_race_3.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
// -------
// Mutexes
// -------
// We don't always have the luxury of using of 4-8 bytes of memory as a shared data. This is where
// the muxtex comes in. Mutex allows us to have the API like the WaitGroup (Add, Done and Wait)
// where any Goroutine can execute one at a time.
package main
import (
"fmt"
"sync"
)
var (
// counter is a variable incremented by all Goroutines.
counter int
// mutex is used to define a critical section of code.
// Picture mutex as a room where all Goroutines have to go through. However, only one Goroutine
// can go at a time. The scheduler will decide who can get in and which one is next. We cannot
// determine what the scheduler is gonna do. It's gonna be hopefully be fair. Just because one
// Goroutine got to the door before another doesn't that Goroutine got to ended first. Nothing
// here is predictable.
// The key here is, once a Goroutine is allowed in, it must report that it's out.
// All the Goroutines come in will ask for a lock and unlock when it leave for other one to get
// in.
// Two different functions can use the same mutex which means only one Goroutine can execute
// any of given functions at a time.
mutex sync.Mutex
)
func main() {
// Number of Goroutines to use.
const grs = 2
// wg is used to manage concurrency.
var wg sync.WaitGroup
wg.Add(grs)
// Create two Goroutines.
for i := 0; i < grs; i++ {
go func() {
for count := 0; count < 2; count++ {
// Only allow one Goroutine through this critical section at a time.
// Creating these artificial curly brackets gives readability. We don't have to do
// this but it is highly recommended.
// The Lock and Unlock function must always be together in line of sight.
mutex.Lock()
{
// Capture the value of counter.
value := counter
// Increment our local value of counter.
value++
// Store the value back into counter.
counter = value
}
mutex.Unlock()
// Release the lock and allow any waiting Goroutine through.
}
wg.Done()
}()
}
// Wait for the Goroutines to finish.
wg.Wait()
fmt.Printf("Final Counter: %d\n", counter)
}