Created
March 30, 2017 11:09
-
-
Save alextanhongpin/2abf4616989f54b4ec9732a178b0015e to your computer and use it in GitHub Desktop.
Sample mutex 2
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
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