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.

From e988ebce0cf58342bc0bb50a5e39dda69969c278 Mon Sep 17 00:00:00 2001 From: Erazem Kokot Date: Thu, 28 Sep 2023 18:50:55 +0200 Subject: [PATCH 2/3] Run tools/build again --- public/atomic-counters | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/public/atomic-counters b/public/atomic-counters index 1369610f4..1e62f72fd 100644 --- a/public/atomic-counters +++ b/public/atomic-counters @@ -46,7 +46,7 @@ counters accessed by multiple goroutines.

- +
package main
@@ -114,10 +114,7 @@ counter exactly 1000 times.

-

To atomically increment the counter we use Add, -giving it the memory address of our ops counter -with the & syntax.

- + @@ -128,7 +125,10 @@ with the & syntax.

- +

To atomically increment the counter we use Add, +giving it the memory address of our ops counter +with the & syntax.

+ From e6745799a03c8be65fd7648886023531b319d7b1 Mon Sep 17 00:00:00 2001 From: Erazem Kokot Date: Sun, 1 Oct 2023 22:56:19 +0200 Subject: [PATCH 3/3] Fix comments --- examples/atomic-counters/atomic-counters.go | 12 ++++-------- examples/atomic-counters/atomic-counters.hash | 4 ++-- public/atomic-counters | 14 +++++--------- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/examples/atomic-counters/atomic-counters.go b/examples/atomic-counters/atomic-counters.go index dd2e29365..5f8a2d7f4 100644 --- a/examples/atomic-counters/atomic-counters.go +++ b/examples/atomic-counters/atomic-counters.go @@ -31,9 +31,7 @@ func main() { go func() { for c := 0; c < 1000; c++ { - // To atomically increment the counter we use `Add`, - // giving it the memory address of our `ops` counter - // with the `&` syntax. + // To atomically increment the counter we use `Add`. ops.Add(1) } @@ -44,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`. + // 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 17ff05c99..2e18a611d 100644 --- a/examples/atomic-counters/atomic-counters.hash +++ b/examples/atomic-counters/atomic-counters.hash @@ -1,2 +1,2 @@ -8c87658e3694b98a456c73438c453286fe018c28 -5vAUgPMebQw +806f385f4485c3e9d10fe319744dd58ab77adaaf +LfAMxMppwL- diff --git a/public/atomic-counters b/public/atomic-counters index 1e62f72fd..0bd4299c2 100644 --- a/public/atomic-counters +++ b/public/atomic-counters @@ -46,7 +46,7 @@ counters accessed by multiple goroutines.

- +
package main
@@ -125,9 +125,7 @@ counter exactly 1000 times.

-

To atomically increment the counter we use Add, -giving it the memory address of our ops counter -with the & syntax.

+

To atomically increment the counter we use Add.

@@ -162,11 +160,9 @@ with the & syntax.

-

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