std:: atomic_flag_test_and_set, std:: atomic_flag_test_and_set_explicit
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定義済みヘッダー
<atomic>
|
||
|
bool
atomic_flag_test_and_set
(
volatile
std::
atomic_flag
*
obj
)
noexcept
;
|
(1) | (C++11以降) |
|
bool
atomic_flag_test_and_set
(
std::
atomic_flag
*
obj
)
noexcept
;
|
(2) | (C++11以降) |
|
bool
atomic_flag_test_and_set_explicit
(
volatile
std::
atomic_flag
*
obj,
std:: memory_order order ) noexcept ; |
(3) | (C++11以降) |
|
bool
atomic_flag_test_and_set_explicit
(
std::
atomic_flag
*
obj,
std:: memory_order order ) noexcept ; |
(4) | (C++11以降) |
std::atomic_flag が指すオブジェクトの状態をアトミックに設定状態( true )に変更し、変更前の値を返します。
目次 |
パラメータ
| obj | - | アクセスする std::atomic_flag へのポインタ |
| order | - | メモリ同期順序 |
戻り値
obj が指すフラグが以前に保持していた値。
注記
std::atomic_flag_test_and_set
および
std::atomic_flag_test_and_set_explicit
は、それぞれ
obj
-
>
test_and_set
(
)
および
obj
-
>
test_and_set
(
order
)
として実装可能です。
例
スピンロックミューテックスはユーザースペースで
std::atomic_flag
を使用して実装できます。
#include <atomic> #include <iostream> #include <thread> #include <vector> std::atomic_flag lock = ATOMIC_FLAG_INIT; void f(int n) { for (int cnt = 0; cnt < 100; ++cnt) { while (std::atomic_flag_test_and_set_explicit(&lock, std::memory_order_acquire)) ; // spin until the lock is acquired std::cout << "Output from thread " << n << '\n'; std::atomic_flag_clear_explicit(&lock, std::memory_order_release); } } int main() { std::vector<std::thread> v; for (int n = 0; n < 10; ++n) v.emplace_back(f, n); for (auto& t : v) t.join(); }
出力:
Output from thread 2 Output from thread 6 Output from thread 7 ...<exactly 1000 lines>...
関連項目
|
(C++11)
|
ロックフリーなブーリアンアトミック型
(クラス) |
|
(C++11)
(C++11)
|
フラグの値をアトミックに
false
に設定する
(関数) |
|
(C++11)
|
指定されたアトミック操作に対するメモリ順序制約を定義する
(列挙型) |
|
Cドキュメント
for
atomic_flag_test_and_set
,
atomic_flag_test_and_set_explicit
|
|