diff --git a/examples/atomic-counters/atomic-counters.go b/examples/atomic-counters/atomic-counters.go index 046a347..dd2e293 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 f4c10e6..17ff05c 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 fccef50..1369610 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.