title: What does `std::kill_dependency` do, and why would I want to use it?
body: <p>I've been reading about the new C++11 memory model and I've come upon the <code>std::kill_dependency</code> function (&sect;29.3/14-15). I'm struggling to understand why I would ever want to use it.</p>
  
  <p>I found an example in the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2664.htm#selective" rel="noreferrer">N2664 proposal</a> but it didn't help much.</p>
  
  <p>It starts by showing code without <code>std::kill_dependency</code>. Here, the first line carries a dependency into the second, which carries a dependency into the indexing operation, and then carries a dependency into the <code>do_something_with</code> function.</p>
  
  <pre><code>r1 = x.load(memory_order_consume);
  r2 = r1-&gt;index;
  do_something_with(a[r2]);
  </code></pre>
  
  <p>There is further example that uses <code>std::kill_dependency</code> to break the dependency between the second line and the indexing.</p>
  
  <pre><code>r1 = x.load(memory_order_consume);
  r2 = r1-&gt;index;
  do_something_with(a[std::kill_dependency(r2)]);
  </code></pre>
  
  <p>As far as I can tell, this means that the indexing and the call to <code>do_something_with</code> are not dependency ordered before the second line. According to N2664:</p>
  
  <blockquote>
    <p>This allows the compiler to reorder the call to <code>do_something_with</code>, for example, by performing speculative optimizations that predict the value of <code>a[r2]</code>.</p>
  </blockquote>
  
  <p>In order to make the call to <code>do_something_with</code> the value <code>a[r2]</code> is needed. If, hypothetically, the compiler "knows" that the array is filled with zeros, it can optimize that call to <code>do_something_with(0);</code> and reorder this call relative to the other two instructions as it pleases. It could produce any of:</p>
  
  <pre><code>// 1
  r1 = x.load(memory_order_consume);
  r2 = r1-&gt;index;
  do_something_with(0);
  // 2
  r1 = x.load(memory_order_consume);
  do_something_with(0);
  r2 = r1-&gt;index;
  // 3
  do_something_with(0);
  r1 = x.load(memory_order_consume);
  r2 = r1-&gt;index;
  </code></pre>
  
  <p>Is my understanding correct?</p>
  
  <p>If <code>do_something_with</code> synchronizes with another thread by some other means, what does this mean with respect to the ordering of the <code>x.load</code> call and this other thread?</p>
  
  <p>Assuming my understading is correct, there's still one thing that bugs me: when I'm writing code, what reasons would lead me to choose to kill a dependency?</p>