From a8aae649693e6c1a076bdc1959bf0a08552fe135 Mon Sep 17 00:00:00 2001 From: barryz Date: Sat, 8 Sep 2018 20:31:55 +0800 Subject: [PATCH] Refine flip method, may be not atomic yet --- bool.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bool.go b/bool.go index e9e20fa..a57358a 100644 --- a/bool.go +++ b/bool.go @@ -51,7 +51,11 @@ func (ab *AtomicBool) SetTo(yes bool) { // Flip toggles the Boolean (replaces with its opposite value). func (ab *AtomicBool) Flip() { - atomic.StoreInt32((*int32)(ab), atomic.LoadInt32((*int32)(ab))^1) + var o, n int32 + o, n = 0, 1 + if !atomic.CompareAndSwapInt32((*int32)(ab), o, n) { + atomic.CompareAndSwapInt32((*int32)(ab), n, o) + } } // SetToIf sets the Boolean to new only if the Boolean matches the old