On Fri, Nov 29, 2019 at 6:21 PM Liam <networkimp...@gmail.com> wrote:

> Does atomic.AddInt32(&x, 1) always yield unique values for concurrent
> callers?
>
> I'm guessing not, because (I think) I'm seeing that two callers get x+2,
> neither gets x+1.
>

That shouldn't happen, AFAICT. Can you share the code where the incorrect
behavior is occurring? Or, preferably, a simple reproducer program?


> Is there a way to generate unique values with pkg atomic, or is a mutex
> required?
>

Keep in mind that atomic.AddInt32() has the usual two's-complement
overflow semantics. If all you want is a generation counter you really
should be using a uint32 and atomic.AddUint32(). Also, depending on your
preferences and performance considerations you might find it preferable to
use a channel that holds a single int, or small number of ints, that is fed
by a producer goroutine and consumed by any context needing a uniq ID. That
makes it easier to abstract the generation of "unique" ints so that they
satisfy other constraints (e.g., they must be even, odd, prime, etc.).

-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD-ZMmXKE4FANGLH9VnAWbDiM1X4Gf7HfMJvMZVzVNi%3DMA%40mail.gmail.com.

Reply via email to