Merge pull request #270 from mmcgrana/atomics-det

Deterministic example for atomics.
This commit is contained in:
Mark McGranaghan 2019-09-06 07:29:24 -07:00 committed by GitHub
commit fe9f3ab396
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 52 deletions

View File

@ -7,9 +7,11 @@
package main package main
import "fmt" import (
import "time" "fmt"
import "sync/atomic" "sync"
"sync/atomic"
)
func main() { func main() {
@ -17,33 +19,34 @@ func main() {
// (always-positive) counter. // (always-positive) counter.
var ops uint64 var ops uint64
// To simulate concurrent updates, we'll start 50 // A WaitGroup will help us wait for all goroutines
// goroutines that each increment the counter about // to finish their work.
// once a millisecond. var wg sync.WaitGroup
// We'll start 50 goroutines that each increment the
// counter exactly 1000 times.
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
wg.Add(1)
go func() { go func() {
for { for c := 0; c < 1000; c++ {
// To atomically increment the counter we // To atomically increment the counter we
// use `AddUint64`, giving it the memory // use `AddUint64`, giving it the memory
// address of our `ops` counter with the // address of our `ops` counter with the
// `&` syntax. // `&` syntax.
atomic.AddUint64(&ops, 1) atomic.AddUint64(&ops, 1)
// Wait a bit between increments.
time.Sleep(time.Millisecond)
} }
wg.Done()
}() }()
} }
// Wait a second to allow some ops to accumulate. // Wait until all the goroutines are done.
time.Sleep(time.Second) wg.Wait()
// In order to safely use the counter while it's still // It's safe to access `ops` now because we know
// being updated by other goroutines, we extract a // no other goroutine is writing to it. Reading
// copy of the current value into `opsFinal` via // atomics safely while they are being updated is
// `LoadUint64`. As above we need to give this // also possible, using functions like
// function the memory address `&ops` from which to // `atomic.LoadUint64`.
// fetch the value. fmt.Println("ops:", ops)
opsFinal := atomic.LoadUint64(&ops)
fmt.Println("ops:", opsFinal)
} }

View File

@ -1,2 +1,2 @@
a4190094ea0405b5f2733101beb97939a1d43aee 8ebec0be3b167021c96b8b497d0e8c0a2ea99385
KDr9EMMPMgi F2pJfduyQiA

View File

@ -1,7 +1,11 @@
# Running the program shows that we executed about # We expect to get exactly 50,000 operations. Had we
# 40,000 operations. # used the non-atomic `ops++` to increment the counter,
# we'd likely get a different number, changing between
# runs, because the goroutines would interfere with
# each other. Moreover, we'd get data race failures
# when running with the `-race` flag.
$ go run atomic-counters.go $ go run atomic-counters.go
ops: 41419 ops: 50000
# Next we'll look at mutexes, another tool for managing # Next we'll look at mutexes, another tool for managing
# state. # state.

69
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="http://play.golang.org/p/KDr9EMMPMgi"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" /> <a href="http://play.golang.org/p/F2pJfduyQiA"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span> <div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div> </pre></div>
@ -59,9 +59,11 @@ counters</em> accessed by multiple goroutines.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="s">&quot;fmt&quot;</span> <div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
<span class="kn">import</span> <span class="s">&quot;time&quot;</span> <span class="s">&quot;fmt&quot;</span>
<span class="kn">import</span> <span class="s">&quot;sync/atomic&quot;</span> <span class="s">&quot;sync&quot;</span>
<span class="s">&quot;sync/atomic&quot;</span>
<span class="p">)</span>
</pre></div> </pre></div>
</td> </td>
@ -95,16 +97,28 @@ counters</em> accessed by multiple goroutines.</p>
<tr> <tr>
<td class="docs"> <td class="docs">
<p>To simulate concurrent updates, we&rsquo;ll start 50 <p>A WaitGroup will help us wait for all goroutines
goroutines that each increment the counter about to finish their work.</p>
once a millisecond.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="kd">var</span> <span class="nx">wg</span> <span class="nx">sync</span><span class="p">.</span><span class="nx">WaitGroup</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>We&rsquo;ll start 50 goroutines that each increment the
counter exactly 1000 times.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<div class="highlight"><pre> <span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="p">&lt;</span> <span class="mi">50</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span> <div class="highlight"><pre> <span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="p">&lt;</span> <span class="mi">50</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
<span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span> <span class="nx">wg</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">for</span> <span class="p">{</span>
</pre></div> </pre></div>
</td> </td>
@ -120,7 +134,8 @@ address of our <code>ops</code> counter with the
</td> </td>
<td class="code leading"> <td class="code leading">
<div class="highlight"><pre> <span class="nx">atomic</span><span class="p">.</span><span class="nx">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> <div class="highlight"><pre> <span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
<span class="k">for</span> <span class="nx">c</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">c</span> <span class="p">&lt;</span> <span class="mi">1000</span><span class="p">;</span> <span class="nx">c</span><span class="o">++</span> <span class="p">{</span>
</pre></div> </pre></div>
</td> </td>
@ -128,13 +143,13 @@ address of our <code>ops</code> counter with the
<tr> <tr>
<td class="docs"> <td class="docs">
<p>Wait a bit between increments.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<div class="highlight"><pre> <span class="nx">time</span><span class="p">.</span><span class="nx">Sleep</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Millisecond</span><span class="p">)</span> <div class="highlight"><pre> <span class="nx">atomic</span><span class="p">.</span><span class="nx">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>
<span class="p">}</span> <span class="p">}</span>
<span class="nx">wg</span><span class="p">.</span><span class="nx">Done</span><span class="p">()</span>
<span class="p">}()</span> <span class="p">}()</span>
<span class="p">}</span> <span class="p">}</span>
</pre></div> </pre></div>
@ -144,12 +159,12 @@ address of our <code>ops</code> counter with the
<tr> <tr>
<td class="docs"> <td class="docs">
<p>Wait a second to allow some ops to accumulate.</p> <p>Wait until all the goroutines are done.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<div class="highlight"><pre> <span class="nx">time</span><span class="p">.</span><span class="nx">Sleep</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">)</span> <div class="highlight"><pre> <span class="nx">wg</span><span class="p">.</span><span class="nx">Wait</span><span class="p">()</span>
</pre></div> </pre></div>
</td> </td>
@ -157,18 +172,16 @@ address of our <code>ops</code> counter with the
<tr> <tr>
<td class="docs"> <td class="docs">
<p>In order to safely use the counter while it&rsquo;s still <p>It&rsquo;s safe to access <code>ops</code> now because we know
being updated by other goroutines, we extract a no other goroutine is writing to it. Reading
copy of the current value into <code>opsFinal</code> via atomics safely while they are being updated is
<code>LoadUint64</code>. As above we need to give this also possible, using functions like
function the memory address <code>&amp;ops</code> from which to <code>atomic.LoadUint64</code>.</p>
fetch the value.</p>
</td> </td>
<td class="code"> <td class="code">
<div class="highlight"><pre> <span class="nx">opsFinal</span> <span class="o">:=</span> <span class="nx">atomic</span><span class="p">.</span><span class="nx">LoadUint64</span><span class="p">(</span><span class="o">&amp;</span><span class="nx">ops</span><span class="p">)</span> <div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;ops:&quot;</span><span class="p">,</span> <span class="nx">ops</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;ops:&quot;</span><span class="p">,</span> <span class="nx">opsFinal</span><span class="p">)</span>
<span class="p">}</span> <span class="p">}</span>
</pre></div> </pre></div>
@ -181,14 +194,18 @@ fetch the value.</p>
<tr> <tr>
<td class="docs"> <td class="docs">
<p>Running the program shows that we executed about <p>We expect to get exactly 50,000 operations. Had we
40,000 operations.</p> used the non-atomic <code>ops++</code> to increment the counter,
we&rsquo;d likely get a different number, changing between
runs, because the goroutines would interfere with
each other. Moreover, we&rsquo;d get data race failures
when running with the <code>-race</code> flag.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> go run atomic-counters.go <div class="highlight"><pre><span class="gp">$</span> go run atomic-counters.go
<span class="go">ops: 41419</span> <span class="go">ops: 50000</span>
</pre></div> </pre></div>
</td> </td>
@ -219,7 +236,7 @@ state.</p>
</div> </div>
<script> <script>
var codeLines = []; var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000Aimport \"time\"\u000Aimport \"sync/atomic\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var ops uint64\u000A');codeLines.push(' for i := 0; i \x3C 50; i++ {\u000A go func() {\u000A for {\u000A');codeLines.push(' atomic.AddUint64(&ops, 1)\u000A');codeLines.push(' time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' time.Sleep(time.Second)\u000A');codeLines.push(' opsFinal := atomic.LoadUint64(&ops)\u000A fmt.Println(\"ops:\", opsFinal)\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 uint64\u000A');codeLines.push(' var wg sync.WaitGroup\u000A');codeLines.push(' for i := 0; i \x3C 50; i++ {\u000A wg.Add(1)\u000A');codeLines.push(' go func() {\u000A for c := 0; c \x3C 1000; c++ {\u000A');codeLines.push(' atomic.AddUint64(&ops, 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('');
</script> </script>
<script src="site.js" async></script> <script src="site.js" async></script>
</body> </body>