Spec-Zone.ru › C++

std::kill_dependency

Определён в заголовочном файле <atomic>
template< class T >
T kill_dependency( T y ) noexcept;
(начиная с C++11)
(constexpr начиная с C++26)
(устарела в C++26)

Сообщает компилятору, что дерево зависимостей, начатое атомарной операцией загрузки std::memory_order_consume, не продолжается за пределами возвращаемого значения std::kill_dependency; то есть аргумент не передаёт зависимость возвращаемому значению.

Это можно использовать, чтобы избежать ненужных барьеров std::memory_order_acquire, когда цепочка зависимостей выходит за пределы области видимости функции (и функция не имеет атрибута [[carries_dependency]]).

(до C++26)

Просто возвращает y. Этот шаблон функции устарел.

(начиная с C++26)

Параметры

y - выражение, возвращаемое значение которого нужно исключить из дерева зависимостей

Возвращаемое значение

Возвращает y, больше не являющееся частью дерева зависимостей(до C++26).

Примеры

file1.cpp:
struct Foo
{
    int* a;
    int* b;
};

std::atomic<Foo*> foo_head[10];
int foo_array[10][10];

// consume operation starts a dependency chain, which escapes this function
[[carries_dependency]] 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)
{
    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
}

См. также

memory_order
(C++11)
задаёт ограничения на порядок памяти для указанной атомарной операции
(перечисление)
Документация по C для kill_dependency

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/atomic/kill_dependency

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API