Skip to content

Instantly share code, notes, and snippets.

@anyu
Created August 7, 2021 00:10
Show Gist options
  • Save anyu/0d4a44f113d8edf1ced686208f7c04a8 to your computer and use it in GitHub Desktop.
Save anyu/0d4a44f113d8edf1ced686208f7c04a8 to your computer and use it in GitHub Desktop.
// 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