File tree Expand file tree Collapse file tree 5 files changed +174
-0
lines changed
000_temp/52-race-condition Expand file tree Collapse file tree 5 files changed +174
-0
lines changed Original file line number Diff line number Diff line change 1+ package _1
2+
3+ import (
4+ "fmt"
5+ "sync"
6+ "runtime"
7+ )
8+
9+ var counter = 0
10+ var mu sync.Mutex
11+
12+ func main () {
13+
14+
15+ const gs = 4
16+ var wg sync.WaitGroup
17+ wg .Add (gs )
18+
19+ for i := 0 ; i < gs ; i ++ {
20+ go func () {
21+ mu .Lock ()
22+ v := counter
23+ // time.Sleep(time.Second * 2)
24+ runtime .Gosched () // tells runtime to yield processor to other goroutine
25+ v ++
26+ counter = v
27+ mu .Unlock ()
28+ wg .Done ()
29+ }()
30+ }
31+ wg .Wait ()
32+
33+ fmt .Println (counter )
34+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ "runtime"
6+ "sync"
7+ )
8+
9+ var wg sync.WaitGroup
10+ var counter int
11+
12+ func main () {
13+ fmt .Println (runtime .NumCPU ())
14+
15+ const gs = 100
16+ wg .Add (gs )
17+
18+ for i := 0 ; i < gs ; i ++ {
19+ go func () {
20+ v := counter
21+ // time.Sleep(time.Second)
22+ runtime .Gosched ()
23+ v ++
24+ counter = v
25+ wg .Done ()
26+ }()
27+ fmt .Println (runtime .NumGoroutine ())
28+ fmt .Println (runtime .NumCPU ())
29+ }
30+ fmt .Println (runtime .NumGoroutine ())
31+ wg .Wait ()
32+ fmt .Println (counter )
33+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ "runtime"
6+ "sync"
7+ )
8+
9+ func main () {
10+ fmt .Println ("CPUs:" , runtime .NumCPU ())
11+ fmt .Println ("Goroutines:" , runtime .NumGoroutine ())
12+
13+ counter := 0
14+
15+ const gs = 100
16+ var wg sync.WaitGroup
17+ wg .Add (gs )
18+
19+ for i := 0 ; i < gs ; i ++ {
20+ go func () {
21+ v := counter
22+ // time.Sleep(time.Second)
23+ runtime .Gosched ()
24+ v ++
25+ counter = v
26+ wg .Done ()
27+ }()
28+ fmt .Println ("Goroutines:" , runtime .NumGoroutine ())
29+ }
30+ wg .Wait ()
31+ fmt .Println ("Goroutines:" , runtime .NumGoroutine ())
32+ fmt .Println ("count:" , counter )
33+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ "runtime"
6+ "sync"
7+ )
8+
9+ func main () {
10+ fmt .Println ("CPUs:" , runtime .NumCPU ())
11+ fmt .Println ("Goroutines:" , runtime .NumGoroutine ())
12+
13+ counter := 0
14+
15+ const gs = 100
16+ var wg sync.WaitGroup
17+ wg .Add (gs )
18+
19+ var mu sync.Mutex
20+
21+ for i := 0 ; i < gs ; i ++ {
22+ go func () {
23+ mu .Lock ()
24+ v := counter
25+ // time.Sleep(time.Second)
26+ runtime .Gosched ()
27+ v ++
28+ counter = v
29+ mu .Unlock ()
30+ wg .Done ()
31+ }()
32+ fmt .Println ("Goroutines:" , runtime .NumGoroutine ())
33+ }
34+ wg .Wait ()
35+ fmt .Println ("Goroutines:" , runtime .NumGoroutine ())
36+ fmt .Println ("count:" , counter )
37+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ "runtime"
6+ "sync"
7+ )
8+
9+ func main () {
10+ fmt .Println ("CPUs:" , runtime .NumCPU ())
11+ fmt .Println ("Goroutines:" , runtime .NumGoroutine ())
12+
13+ counter := 0
14+
15+ const gs = 100
16+ var wg sync.WaitGroup
17+ wg .Add (gs )
18+
19+ var mu sync.Mutex
20+
21+ for i := 0 ; i < gs ; i ++ {
22+ go func () {
23+ mu .Lock ()
24+ v := counter
25+ // time.Sleep(time.Second)
26+ runtime .Gosched ()
27+ v ++
28+ counter = v
29+ mu .Unlock ()
30+ wg .Done ()
31+ }()
32+ fmt .Println ("Goroutines:" , runtime .NumGoroutine ())
33+ }
34+ wg .Wait ()
35+ fmt .Println ("Goroutines:" , runtime .NumGoroutine ())
36+ fmt .Println ("count:" , counter )
37+ }
You can’t perform that action at this time.
0 commit comments