Last active
April 19, 2020 14:27
-
-
Save minhphong306/b99e92ad3c02c0bfe1930b7f5963864d to your computer and use it in GitHub Desktop.
fix_rc_with_atomic.go
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 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