Mastering-Go-Second-Edition/ch10/atom.go
Mihalis Tsoukalos e7048bd69b without atomic
2019-03-29 08:04:03 +02:00

40 lines
617 B
Go

package main
import (
"flag"
"fmt"
"sync"
"sync/atomic"
)
type atomCounter struct {
val int64
}
func (c *atomCounter) Value() int64 {
return atomic.LoadInt64(&c.val)
}
func main() {
minusX := flag.Int("x", 100, "Goroutines")
minusY := flag.Int("y", 200, "Value")
flag.Parse()
X := *minusX
Y := *minusY
var waitGroup sync.WaitGroup
counter := atomCounter{}
for i := 0; i < X; i++ {
waitGroup.Add(1)
go func(no int) {
defer waitGroup.Done()
for i := 0; i < Y; i++ {
// counter.val++
atomic.AddInt64(&counter.val, 1)
}
}(i)
}
waitGroup.Wait()
fmt.Println(counter.Value())
}