diff options
author | Michael Stapelberg <stapelberg@debian.org> | 2014-06-19 09:22:53 +0200 |
---|---|---|
committer | Michael Stapelberg <stapelberg@debian.org> | 2014-06-19 09:22:53 +0200 |
commit | 8a39ee361feb9bf46d728ff1ba4f07ca1d9610b1 (patch) | |
tree | 4449f2036cccf162e8417cc5841a35815b3e7ac5 /doc/go_mem.html | |
parent | c8bf49ef8a92e2337b69c14b9b88396efe498600 (diff) | |
download | golang-upstream/1.3.tar.gz |
Imported Upstream version 1.3upstream/1.3
Diffstat (limited to 'doc/go_mem.html')
-rw-r--r-- | doc/go_mem.html | 37 |
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> |