Skip to content

Instantly share code, notes, and snippets.

@suzuken
Created February 5, 2015 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzuken/a26fc60fff06f4a88761 to your computer and use it in GitHub Desktop.
Save suzuken/a26fc60fff06f4a88761 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
)
var v int32
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
wg := new(sync.WaitGroup)
// for i := 0; i < 10; i++ {
// wg.Add(1)
// go func() {
// v++
// v++
// v++
// v++
// v++
// v++
// v++
// v++
// v++
// v++
// wg.Done()
// }()
// }
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
atomic.AddInt32(&v, 1)
atomic.AddInt32(&v, 1)
atomic.AddInt32(&v, 1)
atomic.AddInt32(&v, 1)
atomic.AddInt32(&v, 1)
atomic.AddInt32(&v, 1)
atomic.AddInt32(&v, 1)
atomic.AddInt32(&v, 1)
atomic.AddInt32(&v, 1)
atomic.AddInt32(&v, 1)
wg.Done()
}()
}
wg.Wait()
fmt.Println(v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment