FAQ
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

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ian Lance Taylor at Jun 4, 2015 at 4:58 am

    On Wed, Jun 3, 2015 at 6:28 PM, Luis Furquim 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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Harmen B at Jun 4, 2015 at 3:44 pm
    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).
    On Thu, Jun 4, 2015 at 6:58 AM, Ian Lance Taylor wrote:
    On Wed, Jun 3, 2015 at 6:28 PM, Luis Furquim 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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Luis Furquim at Jun 4, 2015 at 2:35 pm
    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!


    --
    Luis Otavio de Colla Furquim

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Harmen B at Jun 4, 2015 at 3:43 pm
    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.
    On Thu, Jun 4, 2015 at 4:34 PM, Luis Furquim wrote:

    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!


    --
    Luis Otavio de Colla Furquim
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Luis Furquim at Jun 4, 2015 at 4:16 pm
    Wow! That's great!
    Thank you Harmen!

    On Thu, Jun 4, 2015 at 12:23 PM, Harmen B wrote:

    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.
    On Thu, Jun 4, 2015 at 4:34 PM, Luis Furquim wrote:

    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!


    --
    Luis Otavio de Colla Furquim

    --
    Luis Otavio de Colla Furquim

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 4, '15 at 1:29a
activeJun 4, '15 at 4:16p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase