From 8e17cca56d40188f59d3202404bf0cdbf8118441 Mon Sep 17 00:00:00 2001
From: Erazem Kokot
Date: Thu, 28 Sep 2023 18:49:19 +0200
Subject: [PATCH 1/3] 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.
---
examples/atomic-counters/atomic-counters.go | 17 +++++-----
examples/atomic-counters/atomic-counters.hash | 4 +--
public/atomic-counters | 32 ++++++++++++-------
3 files changed, 31 insertions(+), 22 deletions(-)
diff --git a/examples/atomic-counters/atomic-counters.go b/examples/atomic-counters/atomic-counters.go
index 046a347f3..dd2e29365 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,13 @@ 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`,
+ // giving it the memory address of our `ops` counter
+ // with the `&` syntax.
+ ops.Add(1)
}
+
wg.Done()
}()
}
@@ -48,5 +49,5 @@ func main() {
// atomics safely while they are being updated is
// also possible, using functions like
// `atomic.LoadUint64`.
- fmt.Println("ops:", ops)
+ fmt.Println("ops:", ops.Load())
}
diff --git a/examples/atomic-counters/atomic-counters.hash b/examples/atomic-counters/atomic-counters.hash
index f4c10e6d5..17ff05c99 100644
--- a/examples/atomic-counters/atomic-counters.hash
+++ b/examples/atomic-counters/atomic-counters.hash
@@ -1,2 +1,2 @@
-7b491b40d56a77b01d8e2bd08366de081a4e8d99
-j-14agntvEO
+8c87658e3694b98a456c73438c453286fe018c28
+5vAUgPMebQw
diff --git a/public/atomic-counters b/public/atomic-counters
index fccef500f..1369610f4 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,10 +114,9 @@ counter exactly 1000 times.
- To atomically increment the counter we
-use AddUint64 , giving it the memory
-address of our ops counter with the
-& syntax.
+ To atomically increment the counter we use Add ,
+giving it the memory address of our ops counter
+with the & syntax.
|
@@ -133,9 +132,18 @@ address of our ops counter with the
|
- atomic.AddUint64(&ops, 1)
- }
- wg.Done()
+ ops.Add(1)
+ }
+
|
+
+
+
+
+
+ |
+
+
+ wg.Done()
}()
}
|
@@ -163,7 +171,7 @@ also possible, using functions like
- fmt.Println("ops:", ops)
+ fmt.Println("ops:", ops.Load())
}
|
@@ -216,7 +224,7 @@ state.