title: Parallel for_each using openmp
body: <p>Why does this code not parallelize std::for_each() when it works perfectly fine with std::sort()?</p>
  
  <p>How do I fix it?</p>
  
  <pre><code>g++ -fopenmp -D_GLIBCXX_PARALLEL=1 -o p p.cc &amp;&amp; time ./p  sort
  </code></pre>
  
  <p>GCC 4.3 on Linux.</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;algorithm&gt;
  #include &lt;vector&gt;
  #include &lt;cstring&gt;
  
  void delay()
  {
          for(int c = 0; c &lt; 1000000; c++) {
      }
  }
  
  int lt(int a, int b)
  {
          delay();
          return a &lt; b;
  }
  
  void noop(int a)
  {
      delay();
  }
  
  int main(int argc, char **argv)
  {
          if (argc &lt; 2) {
                  printf("%s  &lt;sort | for_each&gt;\n", argv[0]);
                  return 1;
      }
  
          std::vector&lt;int&gt; foo(10000);
  
          if (!strcmp(argv[1], "sort")) {
          std::sort(foo.begin(), foo.end(), lt);
      } else if (!strcmp(argv[1], "for_each")) {
                  std::for_each(foo.begin(), foo.end(), noop);
      }
  }
  </code></pre>