MCPcopy
hub / github.com/hoanhan101/ultimate-go / main

Function main

go/concurrency/data_race_3.go:36–74  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

34)
35
36func main() {
37 // Number of Goroutines to use.
38 const grs = 2
39
40 // wg is used to manage concurrency.
41 var wg sync.WaitGroup
42 wg.Add(grs)
43
44 // Create two Goroutines.
45 for i := 0; i < grs; i++ {
46 go func() {
47 for count := 0; count < 2; count++ {
48 // Only allow one Goroutine through this critical section at a time.
49 // Creating these artificial curly brackets gives readability. We don't have to do
50 // this but it is highly recommended.
51 // The Lock and Unlock function must always be together in line of sight.
52 mutex.Lock()
53 {
54 // Capture the value of counter.
55 value := counter
56
57 // Increment our local value of counter.
58 value++
59
60 // Store the value back into counter.
61 counter = value
62 }
63 mutex.Unlock()
64 // Release the lock and allow any waiting Goroutine through.
65 }
66
67 wg.Done()
68 }()
69 }
70
71 // Wait for the Goroutines to finish.
72 wg.Wait()
73 fmt.Printf("Final Counter: %d\n", counter)
74}

Callers

nothing calls this directly

Calls 4

AddMethod · 0.80
LockMethod · 0.65
UnlockMethod · 0.65
WaitMethod · 0.65

Tested by

no test coverage detected