Created
August 7, 2021 00:10
-
-
Save anyu/0d4a44f113d8edf1ced686208f7c04a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simplest mutex example | |
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func main() { | |
count := 0 | |
var m sync.Mutex | |
write := func() { | |
m.Lock() | |
for i := 0; i < 100; i++ { | |
count+=10 | |
} | |
m.Unlock() | |
} | |
go write() | |
go write() | |
time.Sleep(time.Millisecond * 5) | |
fmt.Printf("Count: %v", count) // should be 2000 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment