Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Atomic Counters example to use atomic.Uint64 #490

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions examples/atomic-counters/atomic-counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
erazemk marked this conversation as resolved.
Show resolved Hide resolved
// with the `&` syntax.
ops.Add(1)
}

wg.Done()
}()
}
Expand All @@ -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())
erazemk marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 2 additions & 2 deletions examples/atomic-counters/atomic-counters.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
7b491b40d56a77b01d8e2bd08366de081a4e8d99
j-14agntvEO
8c87658e3694b98a456c73438c453286fe018c28
5vAUgPMebQw
34 changes: 21 additions & 13 deletions public/atomic-counters

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.