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.
 
 
 

863 lines
28 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>atomic - 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 atomic</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/atomic"</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>
</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 atomic provides low-level atomic memory primitives
useful for implementing synchronization algorithms.
</p>
<p>
These functions require great care to be used correctly.
Except for special, low-level applications, synchronization is better
done with channels or the facilities of the sync package.
Share memory by communicating;
don&#39;t communicate by sharing memory.
</p>
<p>
The swap operation, implemented by the SwapT functions, is the atomic
equivalent of:
</p>
<pre>old = *addr
*addr = new
return old
</pre>
<p>
The compare-and-swap operation, implemented by the CompareAndSwapT
functions, is the atomic equivalent of:
</p>
<pre>if *addr == old {
*addr = new
return true
}
return false
</pre>
<p>
The add operation, implemented by the AddT functions, is the atomic
equivalent of:
</p>
<pre>*addr += delta
return *addr
</pre>
<p>
The load and store operations, implemented by the LoadT and StoreT
functions, are the atomic equivalents of &#34;return *addr&#34; and
&#34;*addr = val&#34;.
</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#AddInt32">func AddInt32(addr *int32, delta int32) (new int32)</a></dd>
<dd><a href="index.html#AddInt64">func AddInt64(addr *int64, delta int64) (new int64)</a></dd>
<dd><a href="index.html#AddUint32">func AddUint32(addr *uint32, delta uint32) (new uint32)</a></dd>
<dd><a href="index.html#AddUint64">func AddUint64(addr *uint64, delta uint64) (new uint64)</a></dd>
<dd><a href="index.html#AddUintptr">func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)</a></dd>
<dd><a href="index.html#CompareAndSwapInt32">func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)</a></dd>
<dd><a href="index.html#CompareAndSwapInt64">func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)</a></dd>
<dd><a href="index.html#CompareAndSwapPointer">func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)</a></dd>
<dd><a href="index.html#CompareAndSwapUint32">func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)</a></dd>
<dd><a href="index.html#CompareAndSwapUint64">func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)</a></dd>
<dd><a href="index.html#CompareAndSwapUintptr">func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)</a></dd>
<dd><a href="index.html#LoadInt32">func LoadInt32(addr *int32) (val int32)</a></dd>
<dd><a href="index.html#LoadInt64">func LoadInt64(addr *int64) (val int64)</a></dd>
<dd><a href="index.html#LoadPointer">func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)</a></dd>
<dd><a href="index.html#LoadUint32">func LoadUint32(addr *uint32) (val uint32)</a></dd>
<dd><a href="index.html#LoadUint64">func LoadUint64(addr *uint64) (val uint64)</a></dd>
<dd><a href="index.html#LoadUintptr">func LoadUintptr(addr *uintptr) (val uintptr)</a></dd>
<dd><a href="index.html#StoreInt32">func StoreInt32(addr *int32, val int32)</a></dd>
<dd><a href="index.html#StoreInt64">func StoreInt64(addr *int64, val int64)</a></dd>
<dd><a href="index.html#StorePointer">func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)</a></dd>
<dd><a href="index.html#StoreUint32">func StoreUint32(addr *uint32, val uint32)</a></dd>
<dd><a href="index.html#StoreUint64">func StoreUint64(addr *uint64, val uint64)</a></dd>
<dd><a href="index.html#StoreUintptr">func StoreUintptr(addr *uintptr, val uintptr)</a></dd>
<dd><a href="index.html#SwapInt32">func SwapInt32(addr *int32, new int32) (old int32)</a></dd>
<dd><a href="index.html#SwapInt64">func SwapInt64(addr *int64, new int64) (old int64)</a></dd>
<dd><a href="index.html#SwapPointer">func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)</a></dd>
<dd><a href="index.html#SwapUint32">func SwapUint32(addr *uint32, new uint32) (old uint32)</a></dd>
<dd><a href="index.html#SwapUint64">func SwapUint64(addr *uint64, new uint64) (old uint64)</a></dd>
<dd><a href="index.html#SwapUintptr">func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)</a></dd>
<dd><a href="index.html#Value">type Value</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Value.Load">func (v *Value) Load() (x interface{})</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Value.Store">func (v *Value) Store(x interface{})</a></dd>
<dd><a href="index.html#pkg-note-BUG">Bugs</a></dd>
</dl>
</div><!-- #manual-nav -->
<div id="pkg-examples">
<h4>Examples</h4>
<dl>
<dd><a class="exampleLink" href="index.html#example_Value_config">Value (Config)</a></dd>
<dd><a class="exampleLink" href="index.html#example_Value_readMostly">Value (ReadMostly)</a></dd>
</dl>
</div>
<h4>Package files</h4>
<p>
<span style="font-size:90%">
<a href="http://localhost:6060/src/sync/atomic/doc.go">doc.go</a>
<a href="http://localhost:6060/src/sync/atomic/value.go">value.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="AddInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3560:3611#L82">AddInt32</a></h2>
<pre>func AddInt32(addr *<a href="../../builtin/index.html#int32">int32</a>, delta <a href="../../builtin/index.html#int32">int32</a>) (new <a href="../../builtin/index.html#int32">int32</a>)</pre>
<p>
AddInt32 atomically adds delta to *addr and returns the new value.
</p>
<h2 id="AddInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3965:4016#L90">AddInt64</a></h2>
<pre>func AddInt64(addr *<a href="../../builtin/index.html#int64">int64</a>, delta <a href="../../builtin/index.html#int64">int64</a>) (new <a href="../../builtin/index.html#int64">int64</a>)</pre>
<p>
AddInt64 atomically adds delta to *addr and returns the new value.
</p>
<h2 id="AddUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3838:3893#L87">AddUint32</a></h2>
<pre>func AddUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>, delta <a href="../../builtin/index.html#uint32">uint32</a>) (new <a href="../../builtin/index.html#uint32">uint32</a>)</pre>
<p>
AddUint32 atomically adds delta to *addr and returns the new value.
To subtract a signed positive constant value c from x, do AddUint32(&amp;x, ^uint32(c-1)).
In particular, to decrement x, do AddUint32(&amp;x, ^uint32(0)).
</p>
<h2 id="AddUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4243:4298#L95">AddUint64</a></h2>
<pre>func AddUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>, delta <a href="../../builtin/index.html#uint64">uint64</a>) (new <a href="../../builtin/index.html#uint64">uint64</a>)</pre>
<p>
AddUint64 atomically adds delta to *addr and returns the new value.
To subtract a signed positive constant value c from x, do AddUint64(&amp;x, ^uint64(c-1)).
In particular, to decrement x, do AddUint64(&amp;x, ^uint64(0)).
</p>
<h2 id="AddUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4372:4431#L98">AddUintptr</a></h2>
<pre>func AddUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>, delta <a href="../../builtin/index.html#uintptr">uintptr</a>) (new <a href="../../builtin/index.html#uintptr">uintptr</a>)</pre>
<p>
AddUintptr atomically adds delta to *addr and returns the new value.
</p>
<h2 id="CompareAndSwapInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2608:2676#L64">CompareAndSwapInt32</a></h2>
<pre>func CompareAndSwapInt32(addr *<a href="../../builtin/index.html#int32">int32</a>, old, new <a href="../../builtin/index.html#int32">int32</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
<p>
CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value.
</p>
<h2 id="CompareAndSwapInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2761:2829#L67">CompareAndSwapInt64</a></h2>
<pre>func CompareAndSwapInt64(addr *<a href="../../builtin/index.html#int64">int64</a>, old, new <a href="../../builtin/index.html#int64">int64</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
<p>
CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value.
</p>
<h2 id="CompareAndSwapPointer">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3400:3488#L79">CompareAndSwapPointer</a></h2>
<pre>func CompareAndSwapPointer(addr *<a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>, old, new <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
<p>
CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value.
</p>
<h2 id="CompareAndSwapUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2915:2986#L70">CompareAndSwapUint32</a></h2>
<pre>func CompareAndSwapUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>, old, new <a href="../../builtin/index.html#uint32">uint32</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
<p>
CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value.
</p>
<h2 id="CompareAndSwapUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3072:3143#L73">CompareAndSwapUint64</a></h2>
<pre>func CompareAndSwapUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>, old, new <a href="../../builtin/index.html#uint64">uint64</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
<p>
CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value.
</p>
<h2 id="CompareAndSwapUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3231:3305#L76">CompareAndSwapUintptr</a></h2>
<pre>func CompareAndSwapUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>, old, new <a href="../../builtin/index.html#uintptr">uintptr</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
<p>
CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value.
</p>
<h2 id="LoadInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4470:4509#L101">LoadInt32</a></h2>
<pre>func LoadInt32(addr *<a href="../../builtin/index.html#int32">int32</a>) (val <a href="../../builtin/index.html#int32">int32</a>)</pre>
<p>
LoadInt32 atomically loads *addr.
</p>
<h2 id="LoadInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4548:4587#L104">LoadInt64</a></h2>
<pre>func LoadInt64(addr *<a href="../../builtin/index.html#int64">int64</a>) (val <a href="../../builtin/index.html#int64">int64</a>)</pre>
<p>
LoadInt64 atomically loads *addr.
</p>
<h2 id="LoadPointer">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4878:4937#L116">LoadPointer</a></h2>
<pre>func LoadPointer(addr *<a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>) (val <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>)</pre>
<p>
LoadPointer atomically loads *addr.
</p>
<h2 id="LoadUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4627:4669#L107">LoadUint32</a></h2>
<pre>func LoadUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>) (val <a href="../../builtin/index.html#uint32">uint32</a>)</pre>
<p>
LoadUint32 atomically loads *addr.
</p>
<h2 id="LoadUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4709:4751#L110">LoadUint64</a></h2>
<pre>func LoadUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>) (val <a href="../../builtin/index.html#uint64">uint64</a>)</pre>
<p>
LoadUint64 atomically loads *addr.
</p>
<h2 id="LoadUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4792:4837#L113">LoadUintptr</a></h2>
<pre>func LoadUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>) (val <a href="../../builtin/index.html#uintptr">uintptr</a>)</pre>
<p>
LoadUintptr atomically loads *addr.
</p>
<h2 id="StoreInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4987:5026#L119">StoreInt32</a></h2>
<pre>func StoreInt32(addr *<a href="../../builtin/index.html#int32">int32</a>, val <a href="../../builtin/index.html#int32">int32</a>)</pre>
<p>
StoreInt32 atomically stores val into *addr.
</p>
<h2 id="StoreInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5076:5115#L122">StoreInt64</a></h2>
<pre>func StoreInt64(addr *<a href="../../builtin/index.html#int64">int64</a>, val <a href="../../builtin/index.html#int64">int64</a>)</pre>
<p>
StoreInt64 atomically stores val into *addr.
</p>
<h2 id="StorePointer">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5450:5509#L134">StorePointer</a></h2>
<pre>func StorePointer(addr *<a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>, val <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>)</pre>
<p>
StorePointer atomically stores val into *addr.
</p>
<h2 id="StoreUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5166:5208#L125">StoreUint32</a></h2>
<pre>func StoreUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>, val <a href="../../builtin/index.html#uint32">uint32</a>)</pre>
<p>
StoreUint32 atomically stores val into *addr.
</p>
<h2 id="StoreUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5259:5301#L128">StoreUint64</a></h2>
<pre>func StoreUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>, val <a href="../../builtin/index.html#uint64">uint64</a>)</pre>
<p>
StoreUint64 atomically stores val into *addr.
</p>
<h2 id="StoreUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5353:5398#L131">StoreUintptr</a></h2>
<pre>func StoreUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>, val <a href="../../builtin/index.html#uintptr">uintptr</a>)</pre>
<p>
StoreUintptr atomically stores val into *addr.
</p>
<h2 id="SwapInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=1742:1792#L46">SwapInt32</a></h2>
<pre>func SwapInt32(addr *<a href="../../builtin/index.html#int32">int32</a>, new <a href="../../builtin/index.html#int32">int32</a>) (old <a href="../../builtin/index.html#int32">int32</a>)</pre>
<p>
SwapInt32 atomically stores new into *addr and returns the previous *addr value.
</p>
<h2 id="SwapInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=1878:1928#L49">SwapInt64</a></h2>
<pre>func SwapInt64(addr *<a href="../../builtin/index.html#int64">int64</a>, new <a href="../../builtin/index.html#int64">int64</a>) (old <a href="../../builtin/index.html#int64">int64</a>)</pre>
<p>
SwapInt64 atomically stores new into *addr and returns the previous *addr value.
</p>
<h2 id="SwapPointer">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2444:2523#L61">SwapPointer</a></h2>
<pre>func SwapPointer(addr *<a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>, new <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>) (old <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>)</pre>
<p>
SwapPointer atomically stores new into *addr and returns the previous *addr value.
</p>
<h2 id="SwapUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2015:2069#L52">SwapUint32</a></h2>
<pre>func SwapUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>, new <a href="../../builtin/index.html#uint32">uint32</a>) (old <a href="../../builtin/index.html#uint32">uint32</a>)</pre>
<p>
SwapUint32 atomically stores new into *addr and returns the previous *addr value.
</p>
<h2 id="SwapUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2156:2210#L55">SwapUint64</a></h2>
<pre>func SwapUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>, new <a href="../../builtin/index.html#uint64">uint64</a>) (old <a href="../../builtin/index.html#uint64">uint64</a>)</pre>
<p>
SwapUint64 atomically stores new into *addr and returns the previous *addr value.
</p>
<h2 id="SwapUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2298:2356#L58">SwapUintptr</a></h2>
<pre>func SwapUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>, new <a href="../../builtin/index.html#uintptr">uintptr</a>) (old <a href="../../builtin/index.html#uintptr">uintptr</a>)</pre>
<p>
SwapUintptr atomically stores new into *addr and returns the previous *addr value.
</p>
<h2 id="Value">type <a href="http://localhost:6060/src/sync/atomic/value.go?s=445:481#L5">Value</a></h2>
<pre>type Value struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
A Value provides an atomic load and store of a consistently typed value.
Values can be created as part of other data structures.
The zero value for a Value returns nil from Load.
Once Store has been called, a Value must not be copied.
</p>
<div id="example_Value_config" class="toggle">
<div class="collapsed">
<p class="exampleHeading toggleButton"><span class="text">Example (Config)</span></p>
</div>
<div class="expanded">
<p class="exampleHeading toggleButton"><span class="text">Example (Config)</span></p>
<p>The following example shows how to use Value for periodic program config updates
and propagation of the changes to worker goroutines.
</p>
<p>Code:</p>
<pre class="code">
var config Value <span class="comment">// holds current server configuration</span>
<span class="comment">// Create initial config value and store into config.</span>
config.Store(loadConfig())
go func() {
<span class="comment">// Reload config every 10 seconds</span>
<span class="comment">// and update config value with the new version.</span>
for {
time.Sleep(10 * time.Second)
config.Store(loadConfig())
}
}()
<span class="comment">// Create worker goroutines that handle incoming requests</span>
<span class="comment">// using the latest config value.</span>
for i := 0; i &lt; 10; i++ {
go func() {
for r := range requests() {
c := config.Load()
<span class="comment">// Handle request r using config c.</span>
_, _ = r, c
}
}()
}
</pre>
</div>
</div>
<div id="example_Value_readMostly" class="toggle">
<div class="collapsed">
<p class="exampleHeading toggleButton"><span class="text">Example (ReadMostly)</span></p>
</div>
<div class="expanded">
<p class="exampleHeading toggleButton"><span class="text">Example (ReadMostly)</span></p>
<p>The following example shows how to maintain a scalable frequently read,
but infrequently updated data structure using copy-on-write idiom.
</p>
<p>Code:</p>
<pre class="code">
type Map map[string]string
var m Value
m.Store(make(Map))
var mu sync.Mutex <span class="comment">// used only by writers</span>
<span class="comment">// read function can be used to read the data without further synchronization</span>
read := func(key string) (val string) {
m1 := m.Load().(Map)
return m1[key]
}
<span class="comment">// insert function can be used to update the data without further synchronization</span>
insert := func(key, val string) {
mu.Lock() <span class="comment">// synchronize with other potential writers</span>
defer mu.Unlock()
m1 := m.Load().(Map) <span class="comment">// load current value of the data structure</span>
m2 := make(Map) <span class="comment">// create a new value</span>
for k, v := range m1 {
m2[k] = v <span class="comment">// copy all data from the current object to the new one</span>
}
m2[key] = val <span class="comment">// do the update that we need</span>
m.Store(m2) <span class="comment">// atomically replace the current object with the new one</span>
<span class="comment">// At this point all new readers start working with the new version.</span>
<span class="comment">// The old version will be garbage collected once the existing readers</span>
<span class="comment">// (if any) are done with it.</span>
}
_, _ = read, insert
</pre>
</div>
</div>
<h3 id="Value.Load">func (*Value) <a href="http://localhost:6060/src/sync/atomic/value.go?s=732:770#L17">Load</a></h3>
<pre>func (v *<a href="index.html#Value">Value</a>) Load() (x interface{})</pre>
<p>
Load returns the value set by the most recent Store.
It returns nil if there has been no call to Store for this Value.
</p>
<h3 id="Value.Store">func (*Value) <a href="http://localhost:6060/src/sync/atomic/value.go?s=1242:1278#L34">Store</a></h3>
<pre>func (v *<a href="index.html#Value">Value</a>) Store(x interface{})</pre>
<p>
Store sets the value of the Value to x.
All calls to Store for a given Value must use values of the same concrete type.
Store of an inconsistent type panics, as does Store(nil).
</p>
<h2 id="pkg-note-BUG">Bugs</h2>
<ul style="list-style: none; padding: 0;">
<li><a href="http://localhost:6060/src/sync/atomic/doc.go?s=1207:1656#L36">&#x261e;</a> On x86-32, the 64-bit functions use instructions unavailable before the Pentium MMX.
On non-Linux ARM, the 64-bit functions use instructions unavailable before the ARMv6k core.
On both ARM and x86-32, it is the caller&#39;s responsibility to arrange for 64-bit
alignment of 64-bit words accessed atomically. The first word in a global
variable or in an allocated struct or slice can be relied upon to be
64-bit aligned.
</li>
</ul>
<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>