Add "Waiting For Goroutines To Finish" example.
Also link to it from the Channel Synchronization example
This commit is contained in:
parent
c43c319d26
commit
6ab81bdf71
@ -23,6 +23,7 @@ Goroutines
|
|||||||
Channels
|
Channels
|
||||||
Channel Buffering
|
Channel Buffering
|
||||||
Channel Synchronization
|
Channel Synchronization
|
||||||
|
Waiting For Goroutines To Finish
|
||||||
Channel Directions
|
Channel Directions
|
||||||
Select
|
Select
|
||||||
Timeouts
|
Timeouts
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
// We can use channels to synchronize execution
|
// We can use channels to synchronize execution
|
||||||
// across goroutines. Here's an example of using a
|
// across goroutines. Here's an example of using a
|
||||||
// blocking receive to wait for a goroutine to finish.
|
// blocking receive to wait for a goroutine to finish.
|
||||||
|
// When waiting for multiple goroutines to finish,
|
||||||
|
// you may prefer to [use a WaitGroup](waiting-for-goroutines-to-finish).
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
fe3e2ea1a67d0f95ce4cb18f3e8aa16d416de0ce
|
df90432a53832c045472981a9da23d1139fb2b5c
|
||||||
0DfW-1RMqi
|
f6qTtt1agmN
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
// To wait for multiple goroutines to finish, we can
|
||||||
|
// use a sync.WaitGroup.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This is the function we'll run in every goroutine.
|
||||||
|
// wg is the WaitGroup it uses to notify that it's done.
|
||||||
|
// Note that a WaitGroup must be passed to functions by
|
||||||
|
// pointer.
|
||||||
|
func worker(id int, wg *sync.WaitGroup) {
|
||||||
|
fmt.Printf("Worker %d starting\n", id)
|
||||||
|
|
||||||
|
// Sleep for a random duration between 500-700 ms
|
||||||
|
// to simulate work. See the [random numbers](random-numbers)
|
||||||
|
// example for more details on *rand*.
|
||||||
|
msToSleep := time.Duration(500 + rand.Intn(200))
|
||||||
|
time.Sleep(msToSleep * time.Millisecond)
|
||||||
|
fmt.Printf("Worker %d done\n", id)
|
||||||
|
|
||||||
|
// Notify the WaitGroup that we're done.
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
// This WaitGroup is used to wait for all the
|
||||||
|
// goroutines launched here to finish.
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
// Launch several goroutines and increment the WorkGroup
|
||||||
|
// counter for each.
|
||||||
|
for i := 1; i <= 5; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go worker(i, &wg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Block until the WorkGroup counter goes back to 0;
|
||||||
|
// all the workers notified they're done.
|
||||||
|
wg.Wait()
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
f068072d11ed9469174c18f5b7a6a7d9d8d3dafb
|
||||||
|
koKzXfbq8kg
|
@ -0,0 +1,14 @@
|
|||||||
|
$ go run waiting-for-goroutines-to-finish.go
|
||||||
|
Worker 5 starting
|
||||||
|
Worker 3 starting
|
||||||
|
Worker 4 starting
|
||||||
|
Worker 1 starting
|
||||||
|
Worker 2 starting
|
||||||
|
Worker 4 done
|
||||||
|
Worker 1 done
|
||||||
|
Worker 2 done
|
||||||
|
Worker 5 done
|
||||||
|
Worker 3 done
|
||||||
|
|
||||||
|
# The order of workers starting up and finishing
|
||||||
|
# is likely to be different for each invocation.
|
@ -27,7 +27,9 @@
|
|||||||
<td class="docs">
|
<td class="docs">
|
||||||
<p>We can use channels to synchronize execution
|
<p>We can use channels to synchronize execution
|
||||||
across goroutines. Here’s an example of using a
|
across goroutines. Here’s an example of using a
|
||||||
blocking receive to wait for a goroutine to finish.</p>
|
blocking receive to wait for a goroutine to finish.
|
||||||
|
When waiting for multiple goroutines to finish,
|
||||||
|
you may prefer to <a href="waiting-for-goroutines-to-finish">use a WaitGroup</a>.</p>
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td class="code empty leading">
|
<td class="code empty leading">
|
||||||
@ -41,7 +43,7 @@ blocking receive to wait for a goroutine to finish.</p>
|
|||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td class="code leading">
|
<td class="code leading">
|
||||||
<a href="http://play.golang.org/p/0DfW-1RMqi"><img title="Run code" src="play.png" class="run" /></a>
|
<a href="http://play.golang.org/p/f6qTtt1agmN"><img title="Run code" src="play.png" class="run" /></a>
|
||||||
<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>
|
||||||
|
|
||||||
@ -169,7 +171,7 @@ started.</p>
|
|||||||
|
|
||||||
|
|
||||||
<p class="next">
|
<p class="next">
|
||||||
Next example: <a href="channel-directions">Channel Directions</a>.
|
Next example: <a href="waiting-for-goroutines-to-finish">Waiting For Goroutines To Finish</a>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class="footer">
|
<p class="footer">
|
||||||
|
@ -85,6 +85,8 @@
|
|||||||
|
|
||||||
<li><a href="channel-synchronization">Channel Synchronization</a></li>
|
<li><a href="channel-synchronization">Channel Synchronization</a></li>
|
||||||
|
|
||||||
|
<li><a href="waiting-for-goroutines-to-finish">Waiting For Goroutines To Finish</a></li>
|
||||||
|
|
||||||
<li><a href="channel-directions">Channel Directions</a></li>
|
<li><a href="channel-directions">Channel Directions</a></li>
|
||||||
|
|
||||||
<li><a href="select">Select</a></li>
|
<li><a href="select">Select</a></li>
|
||||||
|
223
public/waiting-for-goroutines-to-finish
Normal file
223
public/waiting-for-goroutines-to-finish
Normal file
@ -0,0 +1,223 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Go by Example: Waiting For Goroutines To Finish</title>
|
||||||
|
<link rel=stylesheet href="site.css">
|
||||||
|
</head>
|
||||||
|
<script type="text/javascript">
|
||||||
|
if (window.location.host == "gobyexample.com") {
|
||||||
|
var _gaq = _gaq || [];
|
||||||
|
_gaq.push(['_setAccount', 'UA-34996217-1']);
|
||||||
|
_gaq.push(['_trackPageview']);
|
||||||
|
(function() {
|
||||||
|
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
||||||
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
||||||
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<body>
|
||||||
|
<div class="example" id="waiting-for-goroutines-to-finish">
|
||||||
|
<h2><a href="./">Go by Example</a>: Waiting For Goroutines To Finish</h2>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>To wait for multiple goroutines to finish, we can
|
||||||
|
use a sync.WaitGroup.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code empty leading">
|
||||||
|
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
<a href="http://play.golang.org/p/koKzXfbq8kg"><img title="Run code" src="play.png" class="run" /></a>
|
||||||
|
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||||
|
<span class="s">"fmt"</span>
|
||||||
|
<span class="s">"math/rand"</span>
|
||||||
|
<span class="s">"sync"</span>
|
||||||
|
<span class="s">"time"</span>
|
||||||
|
<span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>This is the function we’ll run in every goroutine.
|
||||||
|
wg is the WaitGroup it uses to notify that it’s done.
|
||||||
|
Note that a WaitGroup must be passed to functions by
|
||||||
|
pointer.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">worker</span><span class="p">(</span><span class="nx">id</span> <span class="kt">int</span><span class="p">,</span> <span class="nx">wg</span> <span class="o">*</span><span class="nx">sync</span><span class="p">.</span><span class="nx">WaitGroup</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">"Worker %d starting\n"</span><span class="p">,</span> <span class="nx">id</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Sleep for a random duration between 500-700 ms
|
||||||
|
to simulate work. See the <a href="random-numbers">random numbers</a>
|
||||||
|
example for more details on <em>rand</em>.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">msToSleep</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Duration</span><span class="p">(</span><span class="mi">500</span> <span class="o">+</span> <span class="nx">rand</span><span class="p">.</span><span class="nx">Intn</span><span class="p">(</span><span class="mi">200</span><span class="p">))</span>
|
||||||
|
<span class="nx">time</span><span class="p">.</span><span class="nx">Sleep</span><span class="p">(</span><span class="nx">msToSleep</span> <span class="o">*</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Millisecond</span><span class="p">)</span>
|
||||||
|
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">"Worker %d done\n"</span><span class="p">,</span> <span class="nx">id</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Notify the WaitGroup that we’re done.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">wg</span><span class="p">.</span><span class="nx">Done</span><span class="p">()</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>This WaitGroup is used to wait for all the
|
||||||
|
goroutines launched here to finish.</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>Launch several goroutines and increment the WorkGroup
|
||||||
|
counter for each.</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">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><=</span> <span class="mi">5</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</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">go</span> <span class="nx">worker</span><span class="p">(</span><span class="nx">i</span><span class="p">,</span> <span class="o">&</span><span class="nx">wg</span><span class="p">)</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Block until the WorkGroup counter goes back to 0;
|
||||||
|
all the workers notified they’re done.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">wg</span><span class="p">.</span><span class="nx">Wait</span><span class="p">()</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="gp">$</span> go run waiting-for-goroutines-to-finish.go
|
||||||
|
<span class="go">Worker 5 starting</span>
|
||||||
|
<span class="go">Worker 3 starting</span>
|
||||||
|
<span class="go">Worker 4 starting</span>
|
||||||
|
<span class="go">Worker 1 starting</span>
|
||||||
|
<span class="go">Worker 2 starting</span>
|
||||||
|
<span class="go">Worker 4 done</span>
|
||||||
|
<span class="go">Worker 1 done</span>
|
||||||
|
<span class="go">Worker 2 done</span>
|
||||||
|
<span class="go">Worker 5 done</span>
|
||||||
|
<span class="go">Worker 3 done</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>The order of workers starting up and finishing
|
||||||
|
is likely to be different for each invocation.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code empty">
|
||||||
|
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</table>
|
||||||
|
|
||||||
|
|
||||||
|
<p class="next">
|
||||||
|
Next example: <a href="channel-directions">Channel Directions</a>.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p class="footer">
|
||||||
|
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/waiting-for-goroutines-to-finish">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user