title: Why is this C++11 code containing rand() slower with multiple threads than with one?
body: <p>I'm trying around on the new C++11 threads, but my simple test has abysmal multicore performance. As a simple example, this program adds up some squared random numbers. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;vector&gt;
  #include &lt;cstdlib&gt;
  #include &lt;chrono&gt;
  #include &lt;cmath&gt;
  
  double add_single(int N) {
      double sum=0;
      for (int i = 0; i &lt; N; ++i){
          sum+= sqrt(1.0*rand()/RAND_MAX);
      }
      return sum/N;
  }
  
  void add_multi(int N, double&amp; result) {
      double sum=0;
      for (int i = 0; i &lt; N; ++i){
          sum+= sqrt(1.0*rand()/RAND_MAX);
      }
      result = sum/N;
  }
  
  int main() {
      srand (time(NULL));
      int N = 1000000;
  
      // single-threaded
      auto t1 = std::chrono::high_resolution_clock::now();
      double result1 = add_single(N);
      auto t2 = std::chrono::high_resolution_clock::now();
      auto time_elapsed = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(t2-t1).count();
      std::cout &lt;&lt; "time single: " &lt;&lt; time_elapsed &lt;&lt; std::endl;
  
      // multi-threaded
      std::vector&lt;std::thread&gt; th;
      int nr_threads = 3;
      double partual_results[] = {0,0,0};
      t1 = std::chrono::high_resolution_clock::now();
      for (int i = 0; i &lt; nr_threads; ++i) 
          th.push_back(std::thread(add_multi, N/nr_threads, std::ref(partual_results[i]) ));
      for(auto &amp;a : th)
          a.join();
      double result_multicore = 0;
      for(double result:partual_results)
          result_multicore += result;
      result_multicore /= nr_threads;
      t2 = std::chrono::high_resolution_clock::now();
      time_elapsed = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(t2-t1).count();
      std::cout &lt;&lt; "time multi: " &lt;&lt; time_elapsed &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Compiled with 'g++ -std=c++11 -pthread test.cpp' on Linux and a 3core machine, a typical result is</p>
  
  <pre><code>time single: 33
  time multi: 565
  </code></pre>
  
  <p>So the multi threaded version is more than an order of magnitude slower. I've used random numbers and a sqrt to make the example less trivial and prone to compiler optimizations, so I'm out of ideas.</p>
  
  <p><strong>edit</strong>:</p>
  
  <ol>
  <li>This problem scales for larger N, so the problem is not the short runtime</li>
  <li>The time for creating the threads is not the problem. Excluding it does not change the result significantly</li>
  </ol>
  
  <p>Wow I found the problem. It was indeed rand(). I replaced it with an C++11 equivalent and now the runtime scales perfectly. Thanks everyone!</p>