Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Created March 30, 2017 11:09
Show Gist options
  • Save alextanhongpin/2abf4616989f54b4ec9732a178b0015e to your computer and use it in GitHub Desktop.
Save alextanhongpin/2abf4616989f54b4ec9732a178b0015e to your computer and use it in GitHub Desktop.
Sample mutex 2
package main
import (
"fmt"
"sync"
)
func main() {
m := &sync.Mutex{}
wg := &sync.WaitGroup{}
wg.Add(100)
s := make([]int, 0)
for i := 0; i < 100; i++ {
go update(&s, m, wg)
}
wg.Wait()
fmt.Println(len(s))
}
func update(s *[]int, m *sync.Mutex, wg *sync.WaitGroup) {
m.Lock()
*s = append(*s, 1)
m.Unlock()
wg.Done()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment