std::kill_dependency
来自 cppreference.com
定义在头文件中 <atomic> |
||
template< class T > T kill_dependency( T y ) noexcept; |
(自 C++11 起) | |
告知编译器,由 std::memory_order_consume 原子加载操作启动的依赖树不扩展到 std::kill_dependency
的返回值之外;也就是说,参数不会将依赖关系传递到返回值中。
这可以用来避免在依赖链离开函数范围时(并且函数没有 [[carries_dependency]]
属性)进行不必要的 std::memory_order_acquire 栅栏。
内容 |
[edit] 参数
y | - | 要从依赖树中删除的返回值的表达式 |
[edit] 返回值
返回 y,不再是依赖树的一部分。
[edit] 示例
[edit] 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]); }
[edit] 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 }
[edit] 另请参阅
(C++11) |
为给定的原子操作定义内存排序约束 (枚举) |
C 文档 for kill_dependency
|