sync.mutex start

main
Your Name 2024-07-08 21:07:58 +08:00
parent 0aae887404
commit 1b32e6982a
2 changed files with 30 additions and 0 deletions

View File

@ -2,5 +2,6 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/logging" vcs="Git" />
</component> </component>
</project> </project>

29
sync/mutex.go Normal file
View File

@ -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)
}