On Wed, Feb 27, 2019 at 12:46 PM Serhat Şevki Dinçer <jfcga...@gmail.com> wrote:
>
> I would like to update a counter up to a certain limit atomically, without 
> using a channel or mutex. What do you think of the following (equivalent) 
> functions for sync/atomic?
>
> func CompareAndInc(*addr, Max) bool {
>
>   if *addr < Max {
>     *addr++
>     return true
>   }
>   return false
> }
>
> func CompareAndDec(*addr, Min) bool {
>
>   if *addr > Min {
>     *addr--
>     return true
>   }
>   return false
> }

I think they are too special purpose for sync/atomic.  You can write
your own versions using CompareAndSwap.

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

Reply via email to