Код: Выделить всё
int global_data[]={ ... };
std::atomic index;
void f()
{
int i=index.load(std::memory_order_consume);
do_something_with(global_data[std::kill_dependency(i)]);
}
Код: Выделить всё
predicted_i = ...; // unordered/predicted read of i
i = index.load(std::memory_order_consume); // ordered load of i
global_data = ...; // ordered read of global_data array memory
elem = global_data[predicted_i];
Код: Выделить всё
predicted_global_data = ...; // unordered read of global_data array memory
i = index.load(std::memory_order_consume); // ordered load of i
elem = predicted_global_data[i];
Код: Выделить всё
struct Foo
{
int* a;
int* b;
};
std::atomic 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]);
}
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
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... dependency
Мобильная версия