Skip to content

Instantly share code, notes, and snippets.

@yumed15
Created November 22, 2023 12:12
Show Gist options
  • Save yumed15/8841a76ff900697cd33c381048004028 to your computer and use it in GitHub Desktop.
Save yumed15/8841a76ff900697cd33c381048004028 to your computer and use it in GitHub Desktop.
var count int
var lock sync.Mutex
increment := func() {
lock.Lock() // <--- locking section
defer lock.Unlock() // <--- unlocking
count++
}
decrement := func() {
lock.Lock()
defer lock.Unlock()
count--
}
var arithmetic sync.WaitGroup
for i := 0; i<=5; i++ {
arithmetic.Add(1)
go func() {
defer arithmetic.Done()
increment()
}
}
for i := 0; i<=5; i++ {
arithmetic.Add(1)
go func() {
defer arithmetic.Done()
decrement()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment