diff options
Diffstat (limited to 'doc/go_mem.html')
-rw-r--r-- | doc/go_mem.html | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/doc/go_mem.html b/doc/go_mem.html index 33bce5f7a..da45a07d7 100644 --- a/doc/go_mem.html +++ b/doc/go_mem.html @@ -143,6 +143,35 @@ calling <code>hello</code> will print <code>"hello, world"</code> at some point in the future (perhaps after <code>hello</code> has returned). </p> +<h3>Goroutine destruction</h3> + +<p> +The exit of a goroutine is not guaranteed to happen before +any event in the program. For example, in this program: +</p> + +<pre> +var a string + +func hello() { + go func() { a = "hello" }() + print(a) +} +</pre> + +<p> +the assignment to <code>a</code> is not followed by +any synchronization event, so it is not guaranteed to be +observed by any other goroutine. +In fact, an aggressive compiler might delete the entire <code>go</code> statement. +</p> + +<p> +If the effects of a goroutine must be observed by another goroutine, +use a synchronization mechanism such as a lock or channel +communication to establish a relative ordering. +</p> + <h3>Channel communication</h3> <p> @@ -276,8 +305,9 @@ before the <i>n</i>+1'th call to <code>l.Lock</code>. <h3>Once</h3> <p> -The <code>once</code> package provides a safe mechanism for -initialization in the presence of multiple goroutines. +The <code>sync</code> package provides a safe mechanism for +initialization in the presence of multiple goroutines +through the use of the <code>Once</code> type. Multiple threads can execute <code>once.Do(f)</code> for a particular <code>f</code>, but only one will run <code>f()</code>, and the other calls block until <code>f()</code> has returned. @@ -293,6 +323,7 @@ In this program: <pre> var a string +var once sync.Once func setup() { a = "hello, world" |