A goroutine might try to Close while another is checking IsClosed, it is 
racy no matter how you implement it.
Go with the atomic load.

On Tuesday, October 4, 2016 at 7:32:03 PM UTC+2, Ahmed (OneOfOne) W. wrote:
>
> Some of our code uses something like:
>
> type Dummy struct {
> closed int64
> }
>
> func(d *Dummy) IsClosed() bool {
> return atomic.LoadInt64(&d.closed) == 1
> }
>
> func(d *Dummy) Close() error {
> if !atomic.CompareAndSwapInt64(&d.closed, 0, 1) {
> return fmt.Errorf("already closed")
> }
> // other logic
> return nil
> }
>
> IsClosed feels racy to me, is it better to use 
> atomic.CompareAndSwap(&d.closed, 
> 1, 1) for IsClosed?
>
>
>

-- 
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