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
This commit is contained in:
Erazem Kokot 2023-10-02 14:43:26 +02:00 committed by GitHub
parent 15d8fe75b8
commit d1ca2ce65f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 33 deletions

View File

@ -15,9 +15,9 @@ import (
func main() { func main() {
// We'll use an unsigned integer to represent our // We'll use an atomic integer type to represent our
// (always-positive) counter. // (always-positive) counter.
var ops uint64 var ops atomic.Uint64
// A WaitGroup will help us wait for all goroutines // A WaitGroup will help us wait for all goroutines
// to finish their work. // to finish their work.
@ -30,12 +30,11 @@ func main() {
go func() { go func() {
for c := 0; c < 1000; c++ { for c := 0; c < 1000; c++ {
// To atomically increment the counter we
// use `AddUint64`, giving it the memory // To atomically increment the counter we use `Add`.
// address of our `ops` counter with the ops.Add(1)
// `&` syntax.
atomic.AddUint64(&ops, 1)
} }
wg.Done() wg.Done()
}() }()
} }
@ -43,10 +42,8 @@ func main() {
// Wait until all the goroutines are done. // Wait until all the goroutines are done.
wg.Wait() wg.Wait()
// It's safe to access `ops` now because we know // Reading atomics safely while they are being updated is
// no other goroutine is writing to it. Reading // possible using functions like `Load`, although here it's
// atomics safely while they are being updated is // safe anyway, because no goroutines are writing to 'ops'.
// also possible, using functions like fmt.Println("ops:", ops.Load())
// `atomic.LoadUint64`.
fmt.Println("ops:", ops)
} }

View File

@ -1,2 +1,2 @@
7b491b40d56a77b01d8e2bd08366de081a4e8d99 806f385f4485c3e9d10fe319744dd58ab77adaaf
j-14agntvEO LfAMxMppwL-

40
public/atomic-counters generated
View File

@ -46,7 +46,7 @@ counters</em> accessed by multiple goroutines.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<a href="https://go.dev/play/p/j-14agntvEO"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" /> <a href="https://go.dev/play/p/LfAMxMppwL-"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><code><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span></span></span></code></pre> <pre class="chroma"><code><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span></span></span></code></pre>
</td> </td>
</tr> </tr>
@ -77,13 +77,13 @@ counters</em> accessed by multiple goroutines.</p>
<tr> <tr>
<td class="docs"> <td class="docs">
<p>We&rsquo;ll use an unsigned integer to represent our <p>We&rsquo;ll use an atomic integer type to represent our
(always-positive) counter.</p> (always-positive) counter.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="kd">var</span> <span class="nx">ops</span> <span class="kt">uint64</span></span></span></code></pre> <pre class="chroma"><code><span class="line"><span class="cl"> <span class="kd">var</span> <span class="nx">ops</span> <span class="nx">atomic</span><span class="p">.</span><span class="nx">Uint64</span></span></span></code></pre>
</td> </td>
</tr> </tr>
@ -114,11 +114,7 @@ counter exactly 1000 times.</p>
<tr> <tr>
<td class="docs"> <td class="docs">
<p>To atomically increment the counter we
use <code>AddUint64</code>, giving it the memory
address of our <code>ops</code> counter with the
<code>&amp;</code> syntax.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
@ -127,15 +123,25 @@ address of our <code>ops</code> counter with the
</td> </td>
</tr> </tr>
<tr>
<td class="docs">
<p>To atomically increment the counter we use <code>Add</code>.</p>
</td>
<td class="code leading">
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="nx">ops</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span></span></span></code></pre>
</td>
</tr>
<tr> <tr>
<td class="docs"> <td class="docs">
</td> </td>
<td class="code leading"> <td class="code leading">
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="nx">atomic</span><span class="p">.</span><span class="nf">AddUint64</span><span class="p">(</span><span class="o">&amp;</span><span class="nx">ops</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <pre class="chroma"><code><span class="line"><span class="cl"> <span class="nx">wg</span><span class="p">.</span><span class="nf">Done</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="nx">wg</span><span class="p">.</span><span class="nf">Done</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"> <span class="p">}()</span> </span></span><span class="line"><span class="cl"> <span class="p">}()</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span></span></span></code></pre> </span></span><span class="line"><span class="cl"> <span class="p">}</span></span></span></code></pre>
</td> </td>
@ -154,16 +160,14 @@ address of our <code>ops</code> counter with the
<tr> <tr>
<td class="docs"> <td class="docs">
<p>It&rsquo;s safe to access <code>ops</code> now because we know <p>Reading atomics safely while they are being updated is
no other goroutine is writing to it. Reading possible using functions like <code>Load</code>, although here it&rsquo;s
atomics safely while they are being updated is safe anyway, because no goroutines are writing to &lsquo;ops&rsquo;.</p>
also possible, using functions like
<code>atomic.LoadUint64</code>.</p>
</td> </td>
<td class="code"> <td class="code">
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;ops:&#34;</span><span class="p">,</span> <span class="nx">ops</span><span class="p">)</span> <pre class="chroma"><code><span class="line"><span class="cl"> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;ops:&#34;</span><span class="p">,</span> <span class="nx">ops</span><span class="p">.</span><span class="nf">Load</span><span class="p">())</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre> </span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre>
</td> </td>
</tr> </tr>
@ -216,7 +220,7 @@ state.</p>
</div> </div>
<script> <script>
var codeLines = []; var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sync\"\u000A \"sync/atomic\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var ops uint64\u000A');codeLines.push(' var wg sync.WaitGroup\u000A');codeLines.push(' for i :\u003D 0; i \u003C 50; i++ {\u000A wg.Add(1)\u000A');codeLines.push(' go func() {\u000A for c :\u003D 0; c \u003C 1000; c++ {\u000A');codeLines.push(' atomic.AddUint64(\u0026ops, 1)\u000A }\u000A wg.Done()\u000A }()\u000A }\u000A');codeLines.push(' wg.Wait()\u000A');codeLines.push(' fmt.Println(\"ops:\", ops)\u000A}\u000A');codeLines.push('');codeLines.push(''); codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sync\"\u000A \"sync/atomic\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var ops atomic.Uint64\u000A');codeLines.push(' var wg sync.WaitGroup\u000A');codeLines.push(' for i :\u003D 0; i \u003C 50; i++ {\u000A wg.Add(1)\u000A');codeLines.push(' go func() {\u000A for c :\u003D 0; c \u003C 1000; c++ {\u000A');codeLines.push(' ops.Add(1)\u000A }\u000A');codeLines.push(' wg.Done()\u000A }()\u000A }\u000A');codeLines.push(' wg.Wait()\u000A');codeLines.push(' fmt.Println(\"ops:\", ops.Load())\u000A}\u000A');codeLines.push('');codeLines.push('');
</script> </script>
<script src="site.js" async></script> <script src="site.js" async></script>
</body> </body>