title: Multi-threaded random_r is slower than single threaded version
body: <p>The following program is essentially the same as the one described <a href="http://www.ellipsix.net/blog/post.29.html" rel="noreferrer">here</a>. When I run and compile the program using two threads (NTHREADS == 2), I get the following run times:</p>
  
  <pre><code>real        0m14.120s
  user        0m25.570s
  sys         0m0.050s
  </code></pre>
  
  <p>When it is run with just one thread (NTHREADS == 1), I get run times significantly better even though it is only using one core. </p>
  
  <pre><code>real        0m4.705s
  user        0m4.660s
  sys         0m0.010s
  </code></pre>
  
  <p>My system is dual core, and I know random_r is thread safe and I am pretty sure it is non-blocking. When the same program is run without random_r and a calculation of cosines and sines is used as a replacement, the dual-threaded version runs in about 1/2 the time as expected.  </p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  #define NTHREADS 2
  #define PRNG_BUFSZ 8
  #define ITERATIONS 1000000000
  
  void* thread_run(void* arg) {
      int r1, i, totalIterations = ITERATIONS / NTHREADS;
      for (i = 0; i &lt; totalIterations; i++){
          random_r((struct random_data*)arg, &amp;r1);
      }
      printf("%i\n", r1);
  }
  
  int main(int argc, char** argv) {
      struct random_data* rand_states = (struct random_data*)calloc(NTHREADS, sizeof(struct random_data));
      char* rand_statebufs = (char*)calloc(NTHREADS, PRNG_BUFSZ);
      pthread_t* thread_ids;
      int t = 0;
      thread_ids = (pthread_t*)calloc(NTHREADS, sizeof(pthread_t));
      /* create threads */
      for (t = 0; t &lt; NTHREADS; t++) {
          initstate_r(random(), &amp;rand_statebufs[t], PRNG_BUFSZ, &amp;rand_states[t]);
          pthread_create(&amp;thread_ids[t], NULL, &amp;thread_run, &amp;rand_states[t]);
      }
      for (t = 0; t &lt; NTHREADS; t++) {
          pthread_join(thread_ids[t], NULL);
      }
      free(thread_ids);
      free(rand_states);
      free(rand_statebufs);
  }
  </code></pre>
  
  <p>I am confused why when generating random numbers the two threaded version performs much worse than the single threaded version, considering random_r is meant to be used in multi-threaded applications. </p>