Skip to content

Commit ebec702

Browse files
committed
race conditions
1 parent a57c989 commit ebec702

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)