Skip to content

Instantly share code, notes, and snippets.

@minhphong306
Last active April 19, 2020 14:27
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 minhphong306/b99e92ad3c02c0bfe1930b7f5963864d to your computer and use it in GitHub Desktop.
Save minhphong306/b99e92ad3c02c0bfe1930b7f5963864d to your computer and use it in GitHub Desktop.
fix_rc_with_atomic.go
package main
import (
"fmt"
"sync"
"sync/atomic"
)
var x int64 = 0
func addOne(wg *sync.WaitGroup) {
// Xài hàm của atomic để tăng giá trị.
//x = x + 1
atomic.AddInt64(&x, 1)
wg.Done()
}
func main() {
var w sync.WaitGroup
for i := 0; i < 1000; i++ {
w.Add(1)
go addOne(&w)
}
w.Wait()
fmt.Println("Giá trị của x là: ", x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment