atomic.Store()

199 views
Skip to first unread message

Luis Furquim

unread,
Jun 3, 2015, 9:29:17 PM6/3/15
to golang-nuts
Hi Gophers,

I've been reading the documentation on the atomic package and 
looking some examples available in the internet and still have some 
doubts.

I understand that using atomic.Store() my program can write 
a value and suffer no interruption while doing this. What I
still don't know is if I may run many goroutines that may
eventually write on the same address without "messing" it.
By "messing" it I must specify that there is absolutely no
problem for me if more than one goroutine write on the
same place provided that there's no mixture of the context
written. Specifically I have an array of structs with two int64
fields and the goroutines will write to into different places
of this array. It would be very nice if I could allow them to 
write there and use no mutexes nor channels to control
the access. Again, there is no problem if one goroutine
writes over a place in the array just to have this place written
by another goroutine the very first moment after. The only
thing that must not happen is data written from one goroutine
in one place being mixed with the data from the other goroutine.

For example: 
goroutine 1 writes a[0].Store(mytype{A:3,B:7})
goroutine 2 writes a[0].Store(mytype{A:12,B:1})

It's perfectly ok for me if a[0] ends up with either mytype{A:3,B:7}
or mytype{A:12,B:1}. But trouble comes if a[0] ends up with mytype{A:12,B:7}
or anything like this.

So, using atomic and no mutexes or channel techniques guarantees
data integrity as described above or do I need control the write access
with mutexes / channels?

Thank you in advance

--
Luis Otavio de Colla Furquim

Ian Lance Taylor

unread,
Jun 4, 2015, 12:58:58 AM6/4/15
to luis.f...@gmail.com, golang-nuts
On Wed, Jun 3, 2015 at 6:28 PM, Luis Furquim <luisf...@gmail.com> wrote:
>
> For example:
> goroutine 1 writes a[0].Store(mytype{A:3,B:7})
> goroutine 2 writes a[0].Store(mytype{A:12,B:1})
>
> It's perfectly ok for me if a[0] ends up with either mytype{A:3,B:7}
> or mytype{A:12,B:1}. But trouble comes if a[0] ends up with mytype{A:12,B:7}
> or anything like this.

Yes, that is what atomic.Store guarantees: that you will get precisely
the value you store, and not a mixture of values.

Ian

Luis Furquim

unread,
Jun 4, 2015, 10:35:20 AM6/4/15
to Harmen B, golang-nuts
Thank you Ian and Harmen!

Once Store has been called, a Value must not be copied
Does this means that I can't append the slice *forever* or the limitation is that I can't append *if* there's another goroutine storing something?
In other words, if I guarantee that no goroutine try to store something *when* append runs it would be ok to append slices of values already stored?

Again, thank you!

Harmen B

unread,
Jun 4, 2015, 11:43:13 AM6/4/15
to luis.f...@gmail.com, golang-nuts
I would say you can't ever append, once you've called .Store() on them. You can, however, put pointers to Values in the slice, since then the actual Value won't be the thing which is copied.

Harmen B

unread,
Jun 4, 2015, 11:44:23 AM6/4/15
to luis.f...@gmail.com, golang-nuts
Luis,

just a warning, this is from the docs:

> Once Store has been called, a Value must not be copied.

which means you can't put atomic.Value objects in a slice where you call append() on later (since append() might copy the underlying array).


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luis Furquim

unread,
Jun 4, 2015, 12:16:14 PM6/4/15
to Harmen B, golang-nuts
Wow! That's great!
Thank you Harmen!

Reply all
Reply to author
Forward
0 new messages