I read about the new C ++ 11 memory model, and I came to the std::kill_dependency (& sect; 29.3 / 14-15). I'm struggling to figure out why I would ever want to use it.
I found an example in the N2664 sentence, but that didn't help much.
It starts by displaying the code without std::kill_dependency . Here, the first line transfers the dependency to the second, which carries the dependency in the indexing operation, and then transfers the dependency to the do_something_with function.
r1 = x.load(memory_order_consume); r2 = r1->index; do_something_with(a[r2]);
The following example uses std::kill_dependency to break the dependency between the second row and indexing.
r1 = x.load(memory_order_consume); r2 = r1->index; do_something_with(a[std::kill_dependency(r2)]);
As far as I can tell, this means that indexing and calling do_something_with are not dependent, ordered up to the second line. According to N2664:
This allows the compiler to reorder the do_something_with call, for example, by performing speculative optimizations that predict the value of a[r2] .
To make a do_something_with call, the value of a[r2] required. If, presumably, the compiler “knows” that the array is filled with zeros, it can optimize this call to do_something_with(0); and reorder this call with respect to the other two instructions as you wish. It can produce any of:
// 1 r1 = x.load(memory_order_consume); r2 = r1->index; do_something_with(0); // 2 r1 = x.load(memory_order_consume); do_something_with(0); r2 = r1->index; // 3 do_something_with(0); r1 = x.load(memory_order_consume); r2 = r1->index;
Do I understand correctly?
If do_something_with synchronized with another thread in some other way, what does this mean with respect to streamlining the x.load call and that other thread?
Assuming my underestimation is correct, there is still one thing that bothers me: when I write code, what reasons will lead me to choose to kill addiction?