You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

932 lines
24 KiB

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#375EAB">
<title>sync - The Go Programming Language</title>
<link type="text/css" rel="stylesheet" href="../../lib/godoc/style.css">
<link rel="stylesheet" href="../../lib/godoc/jquery.treeview.css">
<script type="text/javascript">window.initFuncs = [];</script>
</head>
<body>
<div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
...
</div><!-- #lowframe -->
<div id="topbar" class="wide"><div class="container">
<div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
<div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
<a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
<form method="GET" action="http://localhost:6060/search">
<div id="menu">
<a href="http://localhost:6060/doc/">Documents</a>
<a href="http://localhost:6060/pkg/">Packages</a>
<a href="http://localhost:6060/project/">The Project</a>
<a href="http://localhost:6060/help/">Help</a>
<a href="http://localhost:6060/blog/">Blog</a>
<input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
</div>
</form>
</div></div>
<div id="page" class="wide">
<div class="container">
<h1>Package sync</h1>
<div id="nav"></div>
<!--
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<!--
Note: Static (i.e., not template-generated) href and id
attributes start with "pkg-" to make it impossible for
them to conflict with generated attributes (some of which
correspond to Go identifiers).
-->
<script type='text/javascript'>
document.ANALYSIS_DATA = null;
document.CALLGRAPH = null;
</script>
<div id="short-nav">
<dl>
<dd><code>import "sync"</code></dd>
</dl>
<dl>
<dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
<dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
<dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
<dd><a href="index.html#pkg-subdirectories">Subdirectories</a></dd>
</dl>
</div>
<!-- The package's Name is printed as title by the top-level template -->
<div id="pkg-overview" class="toggleVisible">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
</div>
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
<p>
Package sync provides basic synchronization primitives such as mutual
exclusion locks. Other than the Once and WaitGroup types, most are intended
for use by low-level library routines. Higher-level synchronization is
better done via channels and communication.
</p>
<p>
Values containing the types defined in this package should not be copied.
</p>
</div>
</div>
<div id="pkg-index" class="toggleVisible">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
</div>
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
<!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
<div id="manual-nav">
<dl>
<dd><a href="index.html#Cond">type Cond</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewCond">func NewCond(l Locker) *Cond</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Cond.Broadcast">func (c *Cond) Broadcast()</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Cond.Signal">func (c *Cond) Signal()</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Cond.Wait">func (c *Cond) Wait()</a></dd>
<dd><a href="index.html#Locker">type Locker</a></dd>
<dd><a href="index.html#Mutex">type Mutex</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Mutex.Lock">func (m *Mutex) Lock()</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Mutex.Unlock">func (m *Mutex) Unlock()</a></dd>
<dd><a href="index.html#Once">type Once</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Once.Do">func (o *Once) Do(f func())</a></dd>
<dd><a href="index.html#Pool">type Pool</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Pool.Get">func (p *Pool) Get() interface{}</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Pool.Put">func (p *Pool) Put(x interface{})</a></dd>
<dd><a href="index.html#RWMutex">type RWMutex</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#RWMutex.Lock">func (rw *RWMutex) Lock()</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#RWMutex.RLock">func (rw *RWMutex) RLock()</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#RWMutex.RLocker">func (rw *RWMutex) RLocker() Locker</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#RWMutex.RUnlock">func (rw *RWMutex) RUnlock()</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#RWMutex.Unlock">func (rw *RWMutex) Unlock()</a></dd>
<dd><a href="index.html#WaitGroup">type WaitGroup</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#WaitGroup.Add">func (wg *WaitGroup) Add(delta int)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#WaitGroup.Done">func (wg *WaitGroup) Done()</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#WaitGroup.Wait">func (wg *WaitGroup) Wait()</a></dd>
</dl>
</div><!-- #manual-nav -->
<div id="pkg-examples">
<h4>Examples</h4>
<dl>
<dd><a class="exampleLink" href="index.html#example_Once">Once</a></dd>
<dd><a class="exampleLink" href="index.html#example_WaitGroup">WaitGroup</a></dd>
</dl>
</div>
<h4>Package files</h4>
<p>
<span style="font-size:90%">
<a href="http://localhost:6060/src/sync/cond.go">cond.go</a>
<a href="http://localhost:6060/src/sync/mutex.go">mutex.go</a>
<a href="http://localhost:6060/src/sync/once.go">once.go</a>
<a href="http://localhost:6060/src/sync/pool.go">pool.go</a>
<a href="http://localhost:6060/src/sync/runtime.go">runtime.go</a>
<a href="http://localhost:6060/src/sync/rwmutex.go">rwmutex.go</a>
<a href="http://localhost:6060/src/sync/waitgroup.go">waitgroup.go</a>
</span>
</p>
</div><!-- .expanded -->
</div><!-- #pkg-index -->
<div id="pkg-callgraph" class="toggle" style="display: none">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
</div> <!-- .expanded -->
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
<p>
In the call graph viewer below, each node
is a function belonging to this package
and its children are the functions it
calls&mdash;perhaps dynamically.
</p>
<p>
The root nodes are the entry points of the
package: functions that may be called from
outside the package.
There may be non-exported or anonymous
functions among them if they are called
dynamically from another package.
</p>
<p>
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring <code>func</code>
token.
</p>
<p>
Functions may be omitted if they were
determined to be unreachable in the
particular programs or tests that were
analyzed.
</p>
<!-- Zero means show all package entry points. -->
<ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
</div>
</div> <!-- #pkg-callgraph -->
<h2 id="Cond">type <a href="http://localhost:6060/src/sync/cond.go?s=626:789#L13">Cond</a></h2>
<pre>type Cond struct {
<span class="comment">// L is held while observing or changing the condition</span>
L <a href="index.html#Locker">Locker</a>
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
Cond implements a condition variable, a rendezvous point
for goroutines waiting for or announcing the occurrence
of an event.
</p>
<p>
Each Cond has an associated Locker L (often a *Mutex or *RWMutex),
which must be held when changing the condition and
when calling the Wait method.
</p>
<p>
A Cond can be created as part of other structures.
A Cond must not be copied after first use.
</p>
<h3 id="NewCond">func <a href="http://localhost:6060/src/sync/cond.go?s=836:864#L23">NewCond</a></h3>
<pre>func NewCond(l <a href="index.html#Locker">Locker</a>) *<a href="index.html#Cond">Cond</a></pre>
<p>
NewCond returns a new Cond with Locker l.
</p>
<h3 id="Cond.Broadcast">func (*Cond) <a href="http://localhost:6060/src/sync/cond.go?s=1988:2014#L69">Broadcast</a></h3>
<pre>func (c *<a href="index.html#Cond">Cond</a>) Broadcast()</pre>
<p>
Broadcast wakes all goroutines waiting on c.
</p>
<p>
It is allowed but not required for the caller to hold c.L
during the call.
</p>
<h3 id="Cond.Signal">func (*Cond) <a href="http://localhost:6060/src/sync/cond.go?s=1806:1829#L61">Signal</a></h3>
<pre>func (c *<a href="index.html#Cond">Cond</a>) Signal()</pre>
<p>
Signal wakes one goroutine waiting on c, if there is any.
</p>
<p>
It is allowed but not required for the caller to hold c.L
during the call.
</p>
<h3 id="Cond.Wait">func (*Cond) <a href="http://localhost:6060/src/sync/cond.go?s=1446:1467#L43">Wait</a></h3>
<pre>func (c *<a href="index.html#Cond">Cond</a>) Wait()</pre>
<p>
Wait atomically unlocks c.L and suspends execution
of the calling goroutine. After later resuming execution,
Wait locks c.L before returning. Unlike in other systems,
Wait cannot return unless awoken by Broadcast or Signal.
</p>
<p>
Because c.L is not locked when Wait first resumes, the caller
typically cannot assume that the condition is true when
Wait returns. Instead, the caller should Wait in a loop:
</p>
<pre>c.L.Lock()
for !condition() {
c.Wait()
}
... make use of condition ...
c.L.Unlock()
</pre>
<h2 id="Locker">type <a href="http://localhost:6060/src/sync/mutex.go?s=845:888#L18">Locker</a></h2>
<pre>type Locker interface {
Lock()
Unlock()
}</pre>
<p>
A Locker represents an object that can be locked and unlocked.
</p>
<h2 id="Mutex">type <a href="http://localhost:6060/src/sync/mutex.go?s=729:777#L12">Mutex</a></h2>
<pre>type Mutex struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
A Mutex is a mutual exclusion lock.
Mutexes can be created as part of other structures;
the zero value for a Mutex is an unlocked mutex.
</p>
<h3 id="Mutex.Lock">func (*Mutex) <a href="http://localhost:6060/src/sync/mutex.go?s=1095:1117#L32">Lock</a></h3>
<pre>func (m *<a href="index.html#Mutex">Mutex</a>) Lock()</pre>
<p>
Lock locks m.
If the lock is already in use, the calling goroutine
blocks until the mutex is available.
</p>
<h3 id="Mutex.Unlock">func (*Mutex) <a href="http://localhost:6060/src/sync/mutex.go?s=2502:2526#L90">Unlock</a></h3>
<pre>func (m *<a href="index.html#Mutex">Mutex</a>) Unlock()</pre>
<p>
Unlock unlocks m.
It is a run-time error if m is not locked on entry to Unlock.
</p>
<p>
A locked Mutex is not associated with a particular goroutine.
It is allowed for one goroutine to lock a Mutex and then
arrange for another goroutine to unlock it.
</p>
<h2 id="Once">type <a href="http://localhost:6060/src/sync/once.go?s=260:305#L2">Once</a></h2>
<pre>type Once struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
Once is an object that will perform exactly one action.
</p>
<div id="example_Once" class="toggle">
<div class="collapsed">
<p class="exampleHeading toggleButton"><span class="text">Example</span></p>
</div>
<div class="expanded">
<p class="exampleHeading toggleButton"><span class="text">Example</span></p>
<p>Code:</p>
<pre class="code">var once sync.Once
onceBody := func() {
fmt.Println(&#34;Only once&#34;)
}
done := make(chan bool)
for i := 0; i &lt; 10; i++ {
go func() {
once.Do(onceBody)
done &lt;- true
}()
}
for i := 0; i &lt; 10; i++ {
&lt;-done
}
<span class="comment"></pre>
<p>Output:</p>
<pre class="output">Only once
</pre>
</div>
</div>
<h3 id="Once.Do">func (*Once) <a href="http://localhost:6060/src/sync/once.go?s=1139:1166#L25">Do</a></h3>
<pre>func (o *<a href="index.html#Once">Once</a>) Do(f func())</pre>
<p>
Do calls the function f if and only if Do is being called for the
first time for this instance of Once. In other words, given
</p>
<pre>var once Once
</pre>
<p>
if once.Do(f) is called multiple times, only the first call will invoke f,
even if f has a different value in each invocation. A new instance of
Once is required for each function to execute.
</p>
<p>
Do is intended for initialization that must be run exactly once. Since f
is niladic, it may be necessary to use a function literal to capture the
arguments to a function to be invoked by Do:
</p>
<pre>config.once.Do(func() { config.init(filename) })
</pre>
<p>
Because no call to Do returns until the one call to f returns, if f causes
Do to be called, it will deadlock.
</p>
<p>
If f panics, Do considers it to have returned; future calls of Do return
without calling f.
</p>
<h2 id="Pool">type <a href="http://localhost:6060/src/sync/pool.go?s=1587:1930#L33">Pool</a></h2>
<pre>type Pool struct {
<span class="comment">// New optionally specifies a function to generate</span>
<span class="comment">// a value when Get would otherwise return nil.</span>
<span class="comment">// It may not be changed concurrently with calls to Get.</span>
New func() interface{}
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
A Pool is a set of temporary objects that may be individually saved and
retrieved.
</p>
<p>
Any item stored in the Pool may be removed automatically at any time without
notification. If the Pool holds the only reference when this happens, the
item might be deallocated.
</p>
<p>
A Pool is safe for use by multiple goroutines simultaneously.
</p>
<p>
Pool&#39;s purpose is to cache allocated but unused items for later reuse,
relieving pressure on the garbage collector. That is, it makes it easy to
build efficient, thread-safe free lists. However, it is not suitable for all
free lists.
</p>
<p>
An appropriate use of a Pool is to manage a group of temporary items
silently shared among and potentially reused by concurrent independent
clients of a package. Pool provides a way to amortize allocation overhead
across many clients.
</p>
<p>
An example of good use of a Pool is in the fmt package, which maintains a
dynamically-sized store of temporary output buffers. The store scales under
load (when many goroutines are actively printing) and shrinks when
quiescent.
</p>
<p>
On the other hand, a free list maintained as part of a short-lived object is
not a suitable use for a Pool, since the overhead does not amortize well in
that scenario. It is more efficient to have such objects implement their own
free list.
</p>
<h3 id="Pool.Get">func (*Pool) <a href="http://localhost:6060/src/sync/pool.go?s=3041:3073#L84">Get</a></h3>
<pre>func (p *<a href="index.html#Pool">Pool</a>) Get() interface{}</pre>
<p>
Get selects an arbitrary item from the Pool, removes it from the
Pool, and returns it to the caller.
Get may choose to ignore the pool and treat it as empty.
Callers should not assume any relation between values passed to Put and
the values returned by Get.
</p>
<p>
If Get would otherwise return nil and p.New is non-nil, Get returns
the result of calling p.New.
</p>
<h3 id="Pool.Put">func (*Pool) <a href="http://localhost:6060/src/sync/pool.go?s=2221:2254#L52">Put</a></h3>
<pre>func (p *<a href="index.html#Pool">Pool</a>) Put(x interface{})</pre>
<p>
Put adds x to the pool.
</p>
<h2 id="RWMutex">type <a href="http://localhost:6060/src/sync/rwmutex.go?s=479:811#L9">RWMutex</a></h2>
<pre>type RWMutex struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
An RWMutex is a reader/writer mutual exclusion lock.
The lock can be held by an arbitrary number of readers
or a single writer.
RWMutexes can be created as part of other
structures; the zero value for a RWMutex is
an unlocked mutex.
</p>
<h3 id="RWMutex.Lock">func (*RWMutex) <a href="http://localhost:6060/src/sync/rwmutex.go?s=2113:2138#L67">Lock</a></h3>
<pre>func (rw *<a href="index.html#RWMutex">RWMutex</a>) Lock()</pre>
<p>
Lock locks rw for writing.
If the lock is already locked for reading or writing,
Lock blocks until the lock is available.
To ensure that the lock eventually becomes available,
a blocked Lock call excludes new readers from acquiring
the lock.
</p>
<h3 id="RWMutex.RLock">func (*RWMutex) <a href="http://localhost:6060/src/sync/rwmutex.go?s=879:905#L20">RLock</a></h3>
<pre>func (rw *<a href="index.html#RWMutex">RWMutex</a>) RLock()</pre>
<p>
RLock locks rw for reading.
</p>
<h3 id="RWMutex.RLocker">func (*RWMutex) <a href="http://localhost:6060/src/sync/rwmutex.go?s=3660:3695#L120">RLocker</a></h3>
<pre>func (rw *<a href="index.html#RWMutex">RWMutex</a>) RLocker() <a href="index.html#Locker">Locker</a></pre>
<p>
RLocker returns a Locker interface that implements
the Lock and Unlock methods by calling rw.RLock and rw.RUnlock.
</p>
<h3 id="RWMutex.RUnlock">func (*RWMutex) <a href="http://localhost:6060/src/sync/rwmutex.go?s=1346:1374#L39">RUnlock</a></h3>
<pre>func (rw *<a href="index.html#RWMutex">RWMutex</a>) RUnlock()</pre>
<p>
RUnlock undoes a single RLock call;
it does not affect other simultaneous readers.
It is a run-time error if rw is not locked for reading
on entry to RUnlock.
</p>
<h3 id="RWMutex.Unlock">func (*RWMutex) <a href="http://localhost:6060/src/sync/rwmutex.go?s=2960:2987#L93">Unlock</a></h3>
<pre>func (rw *<a href="index.html#RWMutex">RWMutex</a>) Unlock()</pre>
<p>
Unlock unlocks rw for writing. It is a run-time error if rw is
not locked for writing on entry to Unlock.
</p>
<p>
As with Mutexes, a locked RWMutex is not associated with a particular
goroutine. One goroutine may RLock (Lock) an RWMutex and then
arrange for another goroutine to RUnlock (Unlock) it.
</p>
<h2 id="WaitGroup">type <a href="http://localhost:6060/src/sync/waitgroup.go?s=522:830#L8">WaitGroup</a></h2>
<pre>type WaitGroup struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
A WaitGroup waits for a collection of goroutines to finish.
The main goroutine calls Add to set the number of
goroutines to wait for. Then each of the goroutines
runs and calls Done when finished. At the same time,
Wait can be used to block until all goroutines have finished.
</p>
<div id="example_WaitGroup" class="toggle">
<div class="collapsed">
<p class="exampleHeading toggleButton"><span class="text">Example</span></p>
</div>
<div class="expanded">
<p class="exampleHeading toggleButton"><span class="text">Example</span></p>
<p>This example fetches several URLs concurrently,
using a WaitGroup to block until all the fetches are complete.
</p>
<p>Code:</p>
<pre class="code">
var wg sync.WaitGroup
var urls = []string{
&#34;http://www.golang.org/&#34;,
&#34;http://www.google.com/&#34;,
&#34;http://www.somestupidname.com/&#34;,
}
for _, url := range urls {
<span class="comment">// Increment the WaitGroup counter.</span>
wg.Add(1)
<span class="comment">// Launch a goroutine to fetch the URL.</span>
go func(url string) {
<span class="comment">// Decrement the counter when the goroutine completes.</span>
defer wg.Done()
<span class="comment">// Fetch the URL.</span>
http.Get(url)
}(url)
}
<span class="comment">// Wait for all HTTP fetches to complete.</span>
wg.Wait()
</pre>
</div>
</div>
<h3 id="WaitGroup.Add">func (*WaitGroup) <a href="http://localhost:6060/src/sync/waitgroup.go?s=1789:1824#L38">Add</a></h3>
<pre>func (wg *<a href="index.html#WaitGroup">WaitGroup</a>) Add(delta <a href="../builtin/index.html#int">int</a>)</pre>
<p>
Add adds delta, which may be negative, to the WaitGroup counter.
If the counter becomes zero, all goroutines blocked on Wait are released.
If the counter goes negative, Add panics.
</p>
<p>
Note that calls with a positive delta that occur when the counter is zero
must happen before a Wait. Calls with a negative delta, or calls with a
positive delta that start when the counter is greater than zero, may happen
at any time.
Typically this means the calls to Add should execute before the statement
creating the goroutine or other event to be waited for.
If a WaitGroup is reused to wait for several independent sets of events,
new Add calls must happen after all previous Wait calls have returned.
See the WaitGroup example.
</p>
<h3 id="WaitGroup.Done">func (*WaitGroup) <a href="http://localhost:6060/src/sync/waitgroup.go?s=3164:3191#L85">Done</a></h3>
<pre>func (wg *<a href="index.html#WaitGroup">WaitGroup</a>) Done()</pre>
<p>
Done decrements the WaitGroup counter.
</p>
<h3 id="WaitGroup.Wait">func (*WaitGroup) <a href="http://localhost:6060/src/sync/waitgroup.go?s=3261:3288#L90">Wait</a></h3>
<pre>func (wg *<a href="index.html#WaitGroup">WaitGroup</a>) Wait()</pre>
<p>
Wait blocks until the WaitGroup counter is zero.
</p>
<h2 id="pkg-subdirectories">Subdirectories</h2>
<div class="pkg-dir">
<table>
<tr>
<th class="pkg-name">Name</th>
<th class="pkg-synopsis">Synopsis</th>
</tr>
<tr>
<td colspan="2"><a href="http://localhost:6060/pkg/">..</a></td>
</tr>
<tr>
<td class="pkg-name" style="padding-left: 0px;">
<a href="atomic/index.html">atomic</a>
</td>
<td class="pkg-synopsis">
Package atomic provides low-level atomic memory primitives useful for implementing synchronization algorithms.
</td>
</tr>
</table>
</div>
<div id="footer">
Build version go1.6.<br>
Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
the content of this page is licensed under the
Creative Commons Attribution 3.0 License,
and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
<a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
<a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
</div>
</div><!-- .container -->
</div><!-- #page -->
<!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
<script type="text/javascript" src="../../lib/godoc/jquery.js"></script>
<script type="text/javascript" src="../../lib/godoc/jquery.treeview.js"></script>
<script type="text/javascript" src="../../lib/godoc/jquery.treeview.edit.js"></script>
<script type="text/javascript" src="../../lib/godoc/godocs.js"></script>
</body>
</html>