This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatomic.html
2 lines (2 loc) · 22 KB
/
atomic.html
1
2
<title>atomic</title>
<table class='sites-layout-name-one-column sites-layout-hbox' cellspacing='0'><tbody><tr><td class='sites-layout-tile sites-tile-name-content-1'><div dir='ltr'><table style='width:877px' cellspacing='0'><tbody><tr><td><div dir='ltr'><div/><div class='sites-codeblock sites-codesnippet-block'><div><code><font color='#000000'>namespace std {</font></code></div><div><code><font color='#000000'> </font></code><font face='monospace' color='#000000'>template<class T> struct atomic;</font></div><div><font face='monospace' color='#000000'><br/></font></div><div><font face='monospace' color='#000000'> template<> struct atomic<<i>integral</i>>;</font></div><div><font face='monospace' color='#000000'> template<class T> struct atomic<T*>;</font></div><div><code><font color='#000000'>}</font></code></div></div></div></td></tr></tbody></table><h2><a name='TOC--'/>概要</h2><p><code><font color='#000000'>atomic</font></code>クラスは、型<code><font color='#000000'>T</font></code>をアトミック操作するためのクラスである。<br/>整数型およびポインタに対する特殊化が提供されており、それぞれに特化した演算が用意されている。<br/>その他の型に<code><font color='#000000'>atomic</font></code>クラスを使用する場合、型<code><font color='#000000'>T</font></code>はtrivially copyable (TODO)である必要がある。<br/>特殊化された整数型および<code><font color='#000000'>bool</font></code>型にはそれぞれ<code><font color='#000000'>atomic_T</font></code>という名前の<code><font color='#000000'>typedef</font></code>が提供される。<br/><br/></p><table style='border-collapse:collapse;border-color:rgb(136,136,136);border-width:1px' cellspacing='0' bordercolor='#888' border='1'><tbody><tr><td style='width:142px;height:15px'> 名前付きアトミック型</td><td style='width:195px;height:15px'> テンプレート引数となる整数型</td></tr><tr><td style='width:142px;height:15px'><font color='#000000'><code> <font color='#000000'>atomic_char</font></code></font></td><td style='width:195px;height:15px'><code><font color='#000000'> char</font></code></td></tr><tr><td style='width:142px;height:15px'><code><font color='#000000'> atomic_schar</font></code></td><td style='width:195px;height:15px'><code><font color='#000000'> signed char</font></code></td></tr><tr><td style='width:142px;height:15px'><code><font color='#000000'> atomic_uchar</font></code></td><td style='width:195px;height:15px'><code><font color='#000000'> unsigned char</font></code></td></tr><tr><td style='width:142px;height:15px'><code><font color='#000000'> atomic_short</font></code></td><td style='width:195px;height:15px'><code><font color='#000000'> short</font></code></td></tr><tr><td style='width:142px;height:15px'><code><font color='#000000'> atomic_ushort</font></code></td><td style='width:195px;height:15px'><code><font color='#000000'> unsigned short</font></code></td></tr><tr><td style='width:142px;height:15px'><code><font color='#000000'> atomic_int</font></code></td><td style='width:195px;height:15px'><code><font color='#000000'> int</font></code></td></tr><tr><td style='width:142px;height:15px'><code><font color='#000000'> atomic_long</font></code></td><td style='width:195px;height:15px'><code><font color='#000000'> long</font></code></td></tr><tr><td style='width:142px;height:15px'><code><font color='#000000'> atomic_ulong</font></code></td><td style='width:195px;height:15px'><code><font color='#000000'> unsigned long</font></code></td></tr><tr><td style='width:142px;height:15px'><code><font color='#000000'> atomic_llong</font></code></td><td style='width:195px;height:15px'><code><font color='#000000'> long long</font></code></td></tr><tr><td><code><font color='#000000'> atomic_ullong</font></code></td><td><code><font color='#000000'> unsigned long long</font></code></td></tr><tr><td><code><font color='#000000'> atomic_char16_t</font></code></td><td><code><font color='#000000'> char16_t</font></code></td></tr><tr><td><code><font color='#000000'> atomic_char32_t</font></code></td><td><code><font color='#000000'> char32_t</font></code></td></tr><tr><td><code><font color='#000000'> atomic_wchar_t</font></code></td><td><code><font color='#000000'> wchar_t</font></code></td></tr><tr><td><code><font color='#000000'> atomic_bool</font></code></td><td><code><font color='#000000'> bool</font></code></td></tr></tbody></table><br/>また、<inttypes.h>で定義される整数型に対する以下の<code><font color='#000000'>typedef</font></code>も提供される。<br/><br/><table style='border-collapse:collapse;border-color:rgb(136,136,136);border-width:1px' cellspacing='0' bordercolor='#888' border='1'><tbody><tr><td style='width:162px;height:15px'> 名前付きアトミック型</td><td style='width:188px;height:15px'> テンプレート引数となる整数型</td></tr><tr><td style='width:162px;height:12px'><font color='#000000'><code> <font color='#000000'>atomic_int_least8_t</font></code></font></td><td style='width:188px;height:12px'><code><font color='#000000'> int_least8_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uint_least8_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uint_least8_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_int_least16_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> int_least16_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uint_least16_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uint_least16_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_int_least32_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> int_least32_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uint_least32_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uint_least32_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_int_least64_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> int_least64_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uint_least64_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uint_least64_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_int_fast8_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> int_fast8_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uint_fast8_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uint_fast8_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_int_fast16_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> int_fast16_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uint_fast16_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uint_fast16_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_int_fast32_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> int_fast32_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uint_fast32_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uint_fast32_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_int_fast64_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> int_fast64_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uint_fast64_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uint_fast64_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_intptr_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> intptr_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uintptr_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uintptr_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_size_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> size_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_ptrdiff_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> ptrdiff_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_intmax_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> intmax_t</font></code></td></tr><tr><td style='width:162px;height:12px'><code><font color='#000000'> atomic_uintmax_t</font></code></td><td style='width:188px;height:12px'><code><font color='#000000'> uintmax_t</font></code></td></tr></tbody></table><br/><code><font color='#000000'>void*</font></code>に対する特殊化の<code><font color='#000000'>typedef</font></code>として、<code><font color='#000000'>atomic_address</font></code>型が提供される。<br/><h3><a name='TOC-thread-'/></h3><h3><a name='TOC--1'/>共通メンバ関数</h3><table style='border-collapse:collapse;border-color:rgb(136,136,136);border-width:1px' cellspacing='0' bordercolor='#888' border='1'><tbody><tr><td style='width:184px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/atomic'>(constructor)</a></font></code></td><td style='width:296px;height:15px'> コンストラクタ</td></tr><tr><td style='width:184px;height:15px'> <code><font color='#000000'>~atomic() = default</font></code></td><td style='width:296px;height:15px'> デストラクタ</td></tr><tr><td style='width:184px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_assign'>operator=</a></font></code></td><td style='width:296px;height:15px'> 代入演算子</td></tr><tr><td style='width:184px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/is_lock_free'>is_lock_free</a></font></code></td><td style='width:296px;height:15px'> オブジェクトがロックフリーに振る舞えるかを判定する</td></tr><tr><td style='width:184px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/store'>store</a></font></code></td><td style='width:296px;height:15px'> 値を書き込む</td></tr><tr><td style='width:184px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/load'>load</a></font></code></td><td style='width:296px;height:15px'> 値を読み込む</td></tr><tr><td style='width:184px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_t'>operator T</a></font></code></td><td style='width:296px;height:15px'> 型Tへの変換演算子</td></tr><tr><td style='width:184px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/exchange'>exchange</a></font></code></td><td style='width:296px;height:15px'> 値を入れ替える</td></tr><tr><td style='width:184px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/compare_exchange_weak'>compare_exchange_weak</a></font></code></td><td style='width:296px;height:15px'> 弱い比較で値を入れ替える</td></tr><tr><td style='width:184px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/compare_exchange_strong'>compare_exchange_strong</a></font></code></td><td style='width:296px;height:15px'> 強い比較で値を入れ替える</td></tr></tbody></table><h3><a name='TOC--2'/>atomic<integral>専用メンバ関数</h3><table style='border-collapse:collapse;border-top-color:rgb(136,136,136);border-right-color:rgb(136,136,136);border-bottom-color:rgb(136,136,136);border-left-color:rgb(136,136,136);border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px' cellspacing='0' bordercolor='#888' border='1'><tbody><tr><td style='width:112px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/fetch_add'>fetch_add</a></font></code></td><td style='width:286px;height:15px'> 加算</td></tr><tr><td style='width:112px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/fetch_sub'>fetch_sub</a></font></code></td><td style='width:286px;height:15px'> 減算</td></tr><tr><td style='width:112px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/fetch_and'>fetch_and</a></font></code></td><td style='width:286px;height:15px'> AND演算</td></tr><tr><td style='width:112px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/fetch_or'>fetch_or</a></font></code></td><td style='width:286px;height:15px'> OR演算</td></tr><tr><td style='width:112px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/fetch_xor'>fetch_xor</a></font></code></td><td style='width:286px;height:15px'> XOR演算</td></tr><tr><td> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_increment'>operator++</a></font></code></td><td> インクリメント</td></tr><tr><td> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_decrement'>operator--</a></font></code></td><td> デクリメント</td></tr><tr><td> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_plus_assign'>operator+=</a></font></code></td><td> 加算</td></tr><tr><td> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_minus_assign'>operator-=</a></font></code></td><td> 減算</td></tr><tr><td> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_and_assign'>operator&=</a></font></code></td><td> AND演算</td></tr><tr><td> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_or_assign'>operator|=</a></font></code></td><td> OR演算</td></tr><tr><td> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_xor_assign'>operator^=</a></font></code></td><td> XOR演算</td></tr></tbody></table><h3><a name='TOC-atomic-T-'/>atomic<T*>専用メンバ関数</h3><div><table style='border-collapse:collapse;border-top-color:rgb(136,136,136);border-right-color:rgb(136,136,136);border-bottom-color:rgb(136,136,136);border-left-color:rgb(136,136,136);border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px' cellspacing='0' bordercolor='#888' border='1'><tbody><tr><td style='width:113px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/fetch_add'>fetch_add</a></font></code></td><td style='width:283px;height:15px'> 加算</td></tr><tr><td style='width:113px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/fetch_sub'>fetch_sub</a></font></code></td><td style='width:283px;height:15px'> 減算</td></tr><tr><td style='width:113px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_increment'>operator++</a></font></code></td><td style='width:283px;height:15px'> インクリメント</td></tr><tr><td style='width:113px;height:15px'> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_decrement'>operator--</a></font></code></td><td style='width:283px;height:15px'> デクリメント</td></tr><tr><td> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_plus_assign'>operator+=</a></font></code></td><td> 加算</td></tr><tr><td> <code><font color='#000000'><a href='https://sites.google.com/site/cpprefjp/reference/atomic/atomic/op_minus_assign'>operator-=</a></font></code></td><td> 減算</td></tr></tbody></table><h3><a name='TOC--3'/>例</h3></div><div><div/><div class='sites-codeblock sites-codesnippet-block'><div><font face='monospace'><div style='color:rgb(0,0,0)'>// スピンロックの実装</div><div style='color:rgb(0,0,0)'>// Boost Atomic Library - Usage Example</div><div style='color:rgb(0,0,0)'>// <a rel='nofollow' href='http://www.boost.org/doc/libs/1_53_0/doc/html/atomic/usage_examples.html#boost_atomic.usage_examples.example_spinlock'>http://www.boost.org/doc/libs/1_53_0/doc/html/atomic/usage_examples.html#boost_atomic.usage_examples.example_spinlock</a></div><div style='color:rgb(0,0,0)'><br/></div><div style='color:rgb(0,0,0)'>#include <iostream></div><div style='color:rgb(0,0,0)'>#include <atomic></div><div style='color:rgb(0,0,0)'>#include <thread></div><div style='color:rgb(0,0,0)'>#include <mutex></div><div style='color:rgb(0,0,0)'> </div><div style='color:rgb(0,0,0)'>class spinlock {</div><div style='color:rgb(0,0,0)'>private:</div><div style='color:rgb(0,0,0)'> typedef enum {Locked, Unlocked} LockState;</div><div style='color:rgb(0,0,0)'> std::atomic<LockState> state_;</div><div style='color:rgb(0,0,0)'><br/></div><div style='color:rgb(0,0,0)'>public:</div><div style='color:rgb(0,0,0)'> spinlock() : state_(Unlocked) {}</div><div style='color:rgb(0,0,0)'> </div><div style='color:rgb(0,0,0)'> void lock()</div><div style='color:rgb(0,0,0)'> {</div><div style='color:rgb(0,0,0)'> // 現在の状態をLockedと入れ替える</div><div><font color='#000000'> while (state_.</font><font color='#ff0000'>exchange</font><font color='#000000'>(Locked, std::memory_order_acquire) == Locked) {</font></div><div style='color:rgb(0,0,0)'> // busy-wait...アンロックされるまで待機</div><div style='color:rgb(0,0,0)'> }</div><div style='color:rgb(0,0,0)'> }</div><div style='color:rgb(0,0,0)'><br/></div><div style='color:rgb(0,0,0)'> void unlock()</div><div style='color:rgb(0,0,0)'> {</div><div style='color:rgb(0,0,0)'> // 値をUnlockedに更新</div><div><span style='color:rgb(0,0,0)'> state_.</span><font color='#ff0000'>store</font><font color='#000000'>(Unlocked, std::memory_order_release);</font></div><div style='color:rgb(0,0,0)'> }</div><div style='color:rgb(0,0,0)'>};</div><div style='color:rgb(0,0,0)'><br/></div><div style='color:rgb(0,0,0)'>namespace {</div><div style='color:rgb(0,0,0)'> spinlock lock;</div><div style='color:rgb(0,0,0)'>}</div><div style='color:rgb(0,0,0)'><br/></div><div style='color:rgb(0,0,0)'>template <class T></div><div style='color:rgb(0,0,0)'>void print(const T& x)</div><div style='color:rgb(0,0,0)'>{</div><div style='color:rgb(0,0,0)'> std::lock_guard<spinlock> lk(lock);</div><div style='color:rgb(0,0,0)'> std::cout << x << std::endl;</div><div style='color:rgb(0,0,0)'>}</div><div style='color:rgb(0,0,0)'><br/></div><div style='color:rgb(0,0,0)'>void f()</div><div style='color:rgb(0,0,0)'>{</div><div style='color:rgb(0,0,0)'> print(1);</div><div style='color:rgb(0,0,0)'>}</div><div style='color:rgb(0,0,0)'><br/></div><div style='color:rgb(0,0,0)'>void g()</div><div style='color:rgb(0,0,0)'>{</div><div style='color:rgb(0,0,0)'> print(2);</div><div style='color:rgb(0,0,0)'>}</div><div style='color:rgb(0,0,0)'><br/></div><div style='color:rgb(0,0,0)'>int main()</div><div style='color:rgb(0,0,0)'>{</div><div style='color:rgb(0,0,0)'> std::thread t1(f);</div><div style='color:rgb(0,0,0)'> std::thread t2(g);</div><div style='color:rgb(0,0,0)'><br/></div><div style='color:rgb(0,0,0)'> t1.join();</div><div style='color:rgb(0,0,0)'> t2.join();</div><div style='color:rgb(0,0,0)'>}</div></font></div></div></div><h3><a name='TOC--7'/>出力例</h3><div class='sites-codeblock sites-codesnippet-block'><font face='monospace' color='#000000'>2<br/>1</font></div><h2><div style='font-size:13px;font-weight:normal'><h2><a name='TOC--4'/>バージョン</h2><h3><a name='TOC--9'/>言語</h3><div><ul><li>C++11</li></ul></div><div><h3><a name='TOC--10'/>処理系</h3><div><ul><li><a href='https://sites.google.com/site/cpprefjp/implementation#clang'>Clang</a>: ??</li><li><a href='https://sites.google.com/site/cpprefjp/implementation#gcc'>GCC</a>: </li><li><a href='https://sites.google.com/site/cpprefjp/implementation#gcc'>GCC, C++0x mode</a>: 4.7.0(atomic_addressは未実装)</li><li><a href='https://sites.google.com/site/cpprefjp/implementation#icc'>ICC</a>: ??</li><li><a href='https://sites.google.com/site/cpprefjp/implementation#visual_cpp'>Visual C++</a> ??</li></ul></div></div></div><div><h3 style='font-size:13px;font-weight:normal'><a name='TOC--5'/>参照</h3><div style='font-size:13px;font-weight:normal'><a rel='nofollow' href='http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2145.html'>N2145 C++ Atomic Types and Operations</a></div><div><font size='2'><span style='font-weight:normal'><a rel='nofollow' href='http://d.hatena.ne.jp/faith_and_brave/20130110/1357808183'>アトミックオブジェクトを含むクラスのコピーとムーブ - Faith and Brave - C++で遊ぼう</a></span></font></div></div><a name='TOC--8'/></h2></div></td></tr></tbody></table>