summaryrefslogtreecommitdiff
path: root/doc/go_mem.html
diff options
context:
space:
mode:
Diffstat (limited to 'doc/go_mem.html')
-rw-r--r--doc/go_mem.html37
1 files changed, 36 insertions, 1 deletions
diff --git a/doc/go_mem.html b/doc/go_mem.html
index 3e769daec..2ea1ded7a 100644
--- a/doc/go_mem.html
+++ b/doc/go_mem.html
@@ -1,6 +1,6 @@
<!--{
"Title": "The Go Memory Model",
- "Subtitle": "Version of March 6, 2012",
+ "Subtitle": "Version of May 31, 2014",
"Path": "/ref/mem"
}-->
@@ -274,6 +274,41 @@ then the program would not be guaranteed to print
crash, or do something else.)
</p>
+<p class="rule">
+The <i>k</i>th receive on a channel with capacity <i>C</i> happens before the <i>k</i>+<i>C</i>th send from that channel completes.
+</p>
+
+<p>
+This rule generalizes the previous rule to buffered channels.
+It allows a counting semaphore to be modeled by a buffered channel:
+the number of items in the channel corresponds to the number of active uses,
+the capacity of the channel corresponds to the maximum number of simultaneous uses,
+sending an item acquires the semaphore, and receiving an item releases
+the semaphore.
+This is a common idiom for limiting concurrency.
+</p>
+
+<p>
+This program starts a goroutine for every entry in the work list, but the
+goroutines coordinate using the <code>limit</code> channel to ensure
+that at most three are running work functions at a time.
+</p>
+
+<pre>
+var limit = make(chan int, 3)
+
+func main() {
+ for _, w := range work {
+ go func() {
+ limit <- 1
+ w()
+ <-limit
+ }()
+ }
+ select{}
+}
+</pre>
+
<h3>Locks</h3>
<p>