◐ Shell
clean mode source ↗

std::kill_dependency - cppreference.com

提供: cppreference.com

<tbody> </tbody>

template< class T > T kill_dependency( T y ) noexcept;

(C++11以上)

std::memory_order_consume なアトミックロード操作によって開始された依存ツリーが std::kill_dependency の戻り値を超えて伸びない、つまり、引数が戻り値に依存性を伝播しないことを、コンパイラに伝えます。

これは依存連鎖が関数スコープを抜ける (そしてその関数が [[carries_dependency]] 属性を持たない) ときに、不要な std::memory_order_acquire フェンスを回避するために使用することができます。

引数

戻り値

もはや依存ツリーの一部ではない y を返します。

//file1.cpp
struct foo { int* a; int* b; };
std::atomic<struct foo*> foo_head[10];
int foo_array[10][10];

// consume operation starts a dependency chain, which escapes this function
[[carries_dependency]] struct foo* f(int i) {
    return foo_head[i].load(memory_order_consume);
}

// the dependency chain enters this function through the right parameter
// and is killed before the function ends (so no extra acquire operation takes place)
int g(int* x, int* y [[carries_dependency]]) {
    return std::kill_dependency(foo_array[*x][*y]);
}
//file2.cpp
[[carries_dependency]] struct foo* f(int i);
int g(int* x, int* y [[carries_dependency]]);

int c = 3;
void h(int i) {
    struct foo* p;
    p = f(i); // dependency chain started inside f continues into p without undue acquire
    do_something_with(g(&c, p->a)); // p->b is not brought in from the cache
    do_something_with(g(p->a, &c)); // left argument does not have the carries_dependency
                                    // attribute: memory acquire fence may be issued
                                    // p->b becomes visible before g() is entered
}

関連項目