sync.mutex start
parent
0aae887404
commit
1b32e6982a
|
@ -2,5 +2,6 @@
|
|||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/logging" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 互斥锁
|
||||
var x = 0
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var lock sync.Mutex
|
||||
|
||||
func add() {
|
||||
for i := 0; i < 5000; i++ {
|
||||
lock.Lock()
|
||||
x = x + 1
|
||||
lock.Unlock()
|
||||
}
|
||||
wg.Done()
|
||||
}
|
||||
|
||||
func main() {
|
||||
wg.Add(2)
|
||||
go add()
|
||||
go add()
|
||||
wg.Wait()
|
||||
fmt.Println(x)
|
||||
}
|
Loading…
Reference in New Issue