Created
February 9, 2019 02:16
-
-
Save joeke80215/3d5c03628cb2803cd14839fa1ac0340a to your computer and use it in GitHub Desktop.
golang atomic demo
This file contains 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" | |
"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