title: Nested Parallel.ForEach Loops on the same list?
body: <p>I need to parallelize a method that does an exhaustive pairwise comparison on elements in a list. The serial implementation is straight-forward:</p>
  
  <pre><code>foreach (var element1 in list)
      foreach (var element2 in list)
          foo(element1, element2);
  </code></pre>
  
  <p>In this case, foo won't alter the state of element1 or element2. I know it's not safe to simply do nested Parallel.ForEach statements:</p>
  
  <pre><code>Parallel.ForEach(list, delegate(A element1)
  {
      Parallel.ForEach(list, delegate(A element2)
      {
          foo(element1, element2);
      });
  });
  </code></pre>
  
  <p>What would be the ideal way to implement this using the parallel tasks library?</p>