Merge pull request #270 from mmcgrana/atomics-det
Deterministic example for atomics.
This commit is contained in:
commit
fe9f3ab396
@ -7,9 +7,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "time"
|
||||
import "sync/atomic"
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@ -17,33 +19,34 @@ func main() {
|
||||
// (always-positive) counter.
|
||||
var ops uint64
|
||||
|
||||
// To simulate concurrent updates, we'll start 50
|
||||
// goroutines that each increment the counter about
|
||||
// once a millisecond.
|
||||
// A WaitGroup will help us wait for all goroutines
|
||||
// to finish their work.
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// We'll start 50 goroutines that each increment the
|
||||
// counter exactly 1000 times.
|
||||
for i := 0; i < 50; i++ {
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
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)
|
||||
|
||||
// Wait a bit between increments.
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
// Wait a second to allow some ops to accumulate.
|
||||
time.Sleep(time.Second)
|
||||
// Wait until all the goroutines are done.
|
||||
wg.Wait()
|
||||
|
||||
// In order to safely use the counter while it's still
|
||||
// being updated by other goroutines, we extract a
|
||||
// copy of the current value into `opsFinal` via
|
||||
// `LoadUint64`. As above we need to give this
|
||||
// function the memory address `&ops` from which to
|
||||
// fetch the value.
|
||||
opsFinal := atomic.LoadUint64(&ops)
|
||||
fmt.Println("ops:", opsFinal)
|
||||
// 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`.
|
||||
fmt.Println("ops:", ops)
|
||||
}
|
||||
|
@ -1,2 +1,2 @@
|
||||
a4190094ea0405b5f2733101beb97939a1d43aee
|
||||
KDr9EMMPMgi
|
||||
8ebec0be3b167021c96b8b497d0e8c0a2ea99385
|
||||
F2pJfduyQiA
|
||||
|
@ -1,7 +1,11 @@
|
||||
# Running the program shows that we executed about
|
||||
# 40,000 operations.
|
||||
# We expect to get exactly 50,000 operations. Had we
|
||||
# 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
|
||||
ops: 41419
|
||||
ops: 50000
|
||||
|
||||
# Next we'll look at mutexes, another tool for managing
|
||||
# state.
|
||||
|
71
public/atomic-counters
generated
71
public/atomic-counters
generated
@ -46,7 +46,7 @@ counters</em> accessed by multiple goroutines.</p>
|
||||
|
||||
</td>
|
||||
<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>
|
||||
</pre></div>
|
||||
|
||||
@ -59,9 +59,11 @@ counters</em> accessed by multiple goroutines.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"time"</span>
|
||||
<span class="kn">import</span> <span class="s">"sync/atomic"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"sync"</span>
|
||||
<span class="s">"sync/atomic"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -95,16 +97,28 @@ counters</em> accessed by multiple goroutines.</p>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>To simulate concurrent updates, we’ll start 50
|
||||
goroutines that each increment the counter about
|
||||
once a millisecond.</p>
|
||||
<p>A WaitGroup will help us wait for all goroutines
|
||||
to finish their work.</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’ll start 50 goroutines that each increment the
|
||||
counter exactly 1000 times.</p>
|
||||
|
||||
</td>
|
||||
<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"><</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="k">for</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>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -120,7 +134,8 @@ address of our <code>ops</code> counter with the
|
||||
</td>
|
||||
<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">&</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"><</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>
|
||||
|
||||
</td>
|
||||
@ -128,13 +143,13 @@ address of our <code>ops</code> counter with the
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Wait a bit between increments.</p>
|
||||
|
||||
|
||||
</td>
|
||||
<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">&</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="nx">wg</span><span class="p">.</span><span class="nx">Done</span><span class="p">()</span>
|
||||
<span class="p">}()</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
@ -144,12 +159,12 @@ address of our <code>ops</code> counter with the
|
||||
|
||||
<tr>
|
||||
<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 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>
|
||||
|
||||
</td>
|
||||
@ -157,18 +172,16 @@ address of our <code>ops</code> counter with the
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>In order to safely use the counter while it’s still
|
||||
being updated by other goroutines, we extract a
|
||||
copy of the current value into <code>opsFinal</code> via
|
||||
<code>LoadUint64</code>. As above we need to give this
|
||||
function the memory address <code>&ops</code> from which to
|
||||
fetch the value.</p>
|
||||
<p>It’s safe to access <code>ops</code> 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
|
||||
<code>atomic.LoadUint64</code>.</p>
|
||||
|
||||
</td>
|
||||
<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">&</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">"ops:"</span><span class="p">,</span> <span class="nx">opsFinal</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">"ops:"</span><span class="p">,</span> <span class="nx">ops</span><span class="p">)</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
|
||||
@ -181,14 +194,18 @@ fetch the value.</p>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Running the program shows that we executed about
|
||||
40,000 operations.</p>
|
||||
<p>We expect to get exactly 50,000 operations. Had we
|
||||
used the non-atomic <code>ops++</code> 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 <code>-race</code> flag.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<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>
|
||||
|
||||
</td>
|
||||
@ -219,7 +236,7 @@ state.</p>
|
||||
</div>
|
||||
<script>
|
||||
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 src="site.js" async></script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user