C++ 属性: carries_dependency (C++11 以降)(C++26 で削除)
From cppreference.com
< cpp | language | attributes
release-consume std::memory_order の依存関係チェーンが関数の内外に伝播することを示し、コンパイラが不要なメモリフェンス命令をスキップできるようにします。
目次 |
[編集] 構文
[[carries_dependency]]
|
|||||||||
[編集] 説明
この属性は、次の2つの状況で使用できます。
1) 関数の仮引数宣言、またはラムダ式に適用される場合、仮引数の初期化がそのオブジェクトの左辺値から右辺値への変換に依存関係を伝播することを示します。
2) 関数宣言全体に適用される場合、戻り値が関数呼び出し式の評価に依存関係を伝播することを示します。
この属性は、任意の翻訳単位において、関数またはその仮引数の最初の宣言に現れなければなりません。別の翻訳単位において、関数またはその仮引数の最初の宣言にそれが使用されていない場合、プログラムは不正です。診断は不要です。
[編集] 例
SO からほとんど変更なしで採用。
このコードを実行
#include <atomic> #include <iostream> void print(int* val) { std::cout << *val << std::endl; } void print2(int* val [[carries_dependency]]) { std::cout << *val << std::endl; } int main() { int x{42}; std::atomic<int*> p = &x; int* local = p.load(std::memory_order_consume); if (local) { // The dependency is explicit, so the compiler knows that local is // dereferenced, and that it must ensure that the dependency chain // is preserved in order to avoid a fence (on some architectures). std::cout << *local << std::endl; } if (local) { // The definition of print is opaque (assuming it is not inlined), // so the compiler must issue a fence in order to ensure that // reading *p in print returns the correct value. print(local); } if (local) { // The compiler can assume that although print2 is also opaque then // the dependency from the parameter to the dereferenced value is // preserved in the instruction stream, and no fence is necessary (on // some architectures). Obviously, the definition of print2 must actually // preserve this dependency, so the attribute will also impact the // generated code for print2. print2(local); } }
実行結果の例
42 42 42
[編集] 参照
- C++23標準 (ISO/IEC 14882:2024)
- 9.12.4 依存関係属性 [dcl.attr.depend]
- C++20 standard (ISO/IEC 14882:2020)
- 9.12.3 依存関係属性 [dcl.attr.depend]
- C++17 standard (ISO/IEC 14882:2017)
- 10.6.3 依存関係属性 [dcl.attr.depend]
- C++14 standard (ISO/IEC 14882:2014)
- 7.6.4 依存関係属性 [dcl.attr.depend]
- C++11 standard (ISO/IEC 14882:2011)
- 7.6.4 依存関係属性 [dcl.attr.depend]
[編集] 関連項目
| (C++11)(C++26で非推奨) |
std::memory_order_consume 依存関係ツリーから指定されたオブジェクトを削除する (関数テンプレート) |