Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b1407b8

Browse files
committed
race condition demo
1 parent 94ac29b commit b1407b8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎race-condition/main.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
)
7+
8+
// N 是内存里的一个数子
9+
var (
10+
N = 0
11+
waitgroup sync.WaitGroup
12+
)
13+
14+
func counter(number *int) {
15+
*number++
16+
waitgroup.Done()
17+
}
18+
19+
func main() {
20+
21+
for i := 0; i < 1000; i++ {
22+
waitgroup.Add(1)
23+
go counter(&N)
24+
}
25+
waitgroup.Wait()
26+
fmt.Println(N)
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /