Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created August 15, 2018 15:37
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 xeoncross/731d776457620fff10a9724f5e33c39e to your computer and use it in GitHub Desktop.
Save xeoncross/731d776457620fff10a9724f5e33c39e to your computer and use it in GitHub Desktop.
Is there any problem with my assumption that the atomic increments are safe here? https://play.golang.org/p/WpXIcgj1_lQ
package main
import (
"fmt"
"sync/atomic"
"time"
)
type Foo struct {
I *int64
}
func main() {
var counter int64
a := Foo{&counter}
b := Foo{&counter}
c := Foo{&counter}
go func() {
for {
atomic.AddInt64(a.I, 1)
}
}()
go func() {
for {
atomic.AddInt64(b.I, 1)
}
}()
go func() {
for {
atomic.AddInt64(c.I, 1)
}
}()
for i := 0; i < 5; i++ {
time.Sleep(time.Millisecond * 100)
v := atomic.LoadInt64(&counter)
atomic.StoreInt64(&counter, 0)
fmt.Printf("%15d\n", v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment