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 栅栏。
参数
| y | - | 要从依赖树移除其返回值的表达式 | 
返回值
返回 y ,它不再是依赖树的一部分。
示例
- file1.cpp:
 
struct foo { int* a; int* b; }; std::atomic<struct foo*> foo_head[10]; int foo_array[10][10]; // 消费操作开始依赖链,它将脱离此函数 [[carries_dependency]] struct foo* f(int i) { return foo_head[i].load(memory_order_consume); } // 依赖链通过有参数进入此函数,而在函数前被杀掉(故不发生额外的获得操作) 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); // f 内开始的依赖链持续进入 p ,而无过度的获得 do_something_with(g(&c, p->a)); // p->b 不从缓存进口 do_something_with(g(p->a, &c)); // 左参数无 carries_dependency 属性: // 可能发生内存获得栅栏 // p->b 在进入 g() 前变为可见 }
参阅
|    (C++11)  | 
  为给定的原子操作定义内存顺序制约  (枚举)  |