Skip to content

Instantly share code, notes, and snippets.

@joeke80215
Created February 9, 2019 02:16
Show Gist options
  • Save joeke80215/3d5c03628cb2803cd14839fa1ac0340a to your computer and use it in GitHub Desktop.
Save joeke80215/3d5c03628cb2803cd14839fa1ac0340a to your computer and use it in GitHub Desktop.
golang atomic demo
package main
import (
"fmt"
"sync"
"sync/atomic"
)
var browseNum uint32
func addNum() {
atomic.AddUint32(&browseNum, 1)
}
func printNum() {
v := atomic.LoadUint32(&browseNum)
fmt.Println(v)
}
// test command:
// go build -race -v
// .\atomdemo.exe
func main() {
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
addNum()
printNum()
wg.Done()
}()
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment