From d1ca2ce65f0637d3006438ccdd4eeec1d3b842df Mon Sep 17 00:00:00 2001
From: Erazem Kokot
Date: Mon, 2 Oct 2023 14:43:26 +0200
Subject: [PATCH] Update Atomic Counters example to use atomic.Uint64 (#490)
* Update Atomic Counters example to use atomic.Uint64
This commit updates the Atomic Counters example to follow atomic's
recommendation of using atomic.Uint64 instead of uint64 (same for other
types) and then calling methods on it, instead of calling
atomic.AddUint64().
It also updates the comments and empty lines a bit to look better on the
website.
* Run tools/build again
* Fix comments
---
examples/atomic-counters/atomic-counters.go | 23 +++++------
examples/atomic-counters/atomic-counters.hash | 4 +-
public/atomic-counters | 40 ++++++++++---------
3 files changed, 34 insertions(+), 33 deletions(-)
diff --git a/examples/atomic-counters/atomic-counters.go b/examples/atomic-counters/atomic-counters.go
index 046a347..5f8a2d7 100644
--- a/examples/atomic-counters/atomic-counters.go
+++ b/examples/atomic-counters/atomic-counters.go
@@ -15,9 +15,9 @@ import (
func main() {
- // We'll use an unsigned integer to represent our
+ // We'll use an atomic integer type to represent our
// (always-positive) counter.
- var ops uint64
+ var ops atomic.Uint64
// A WaitGroup will help us wait for all goroutines
// to finish their work.
@@ -30,12 +30,11 @@ func main() {
go func() {
for c := 0; c < 1000; c++ {
- // To atomically increment the counter we
- // use `AddUint64`, giving it the memory
- // address of our `ops` counter with the
- // `&` syntax.
- atomic.AddUint64(&ops, 1)
+
+ // To atomically increment the counter we use `Add`.
+ ops.Add(1)
}
+
wg.Done()
}()
}
@@ -43,10 +42,8 @@ func main() {
// Wait until all the goroutines are done.
wg.Wait()
- // It's safe to access `ops` now because we know
- // no other goroutine is writing to it. Reading
- // atomics safely while they are being updated is
- // also possible, using functions like
- // `atomic.LoadUint64`.
- fmt.Println("ops:", ops)
+ // Reading atomics safely while they are being updated is
+ // possible using functions like `Load`, although here it's
+ // safe anyway, because no goroutines are writing to 'ops'.
+ fmt.Println("ops:", ops.Load())
}
diff --git a/examples/atomic-counters/atomic-counters.hash b/examples/atomic-counters/atomic-counters.hash
index f4c10e6..2e18a61 100644
--- a/examples/atomic-counters/atomic-counters.hash
+++ b/examples/atomic-counters/atomic-counters.hash
@@ -1,2 +1,2 @@
-7b491b40d56a77b01d8e2bd08366de081a4e8d99
-j-14agntvEO
+806f385f4485c3e9d10fe319744dd58ab77adaaf
+LfAMxMppwL-
diff --git a/public/atomic-counters b/public/atomic-counters
index fccef50..0bd4299 100644
--- a/public/atomic-counters
+++ b/public/atomic-counters
@@ -46,7 +46,7 @@ counters accessed by multiple goroutines.
- 
+ 
package main
|
@@ -77,13 +77,13 @@ counters accessed by multiple goroutines.
- We’ll use an unsigned integer to represent our
+ We’ll use an atomic integer type to represent our
(always-positive) counter.
|
- var ops uint64
+ var ops atomic.Uint64
|
@@ -114,11 +114,7 @@ counter exactly 1000 times.
- To atomically increment the counter we
-use AddUint64 , giving it the memory
-address of our ops counter with the
-& syntax.
-
+
|
@@ -127,15 +123,25 @@ address of our ops counter with the
|
+
+
+ To atomically increment the counter we use Add .
+
+ |
+
+
+ ops.Add(1)
+ }
+ |
+
+
|
- atomic.AddUint64(&ops, 1)
- }
- wg.Done()
+ wg.Done()
}()
}
|
@@ -154,16 +160,14 @@ address of our ops
counter with the
- It’s safe to access ops now because we know
-no other goroutine is writing to it. Reading
-atomics safely while they are being updated is
-also possible, using functions like
-atomic.LoadUint64 .
+ Reading atomics safely while they are being updated is
+possible using functions like Load , although here it’s
+safe anyway, because no goroutines are writing to ‘ops’.
|
- fmt.Println("ops:", ops)
+ fmt.Println("ops:", ops.Load())
}
|
@@ -216,7 +220,7 @@ state.