title: pthread multithreading in Mac OS vs windows multithreaing
body: <p>I've developed a multi platform program (using the FLTK toolkit) and implement multithreading to do background intensive tasks.</p>
  
  <p>I have followed the FLTK tutorials/examples on multithreading which involve using pthread on Mac, ie the function <code>pthread_create</code> and windows threading on windows ie <code>_beginthread</code></p>
  
  <p>What I have noticed is that the performance is much higher on Windows ie 3 to 4 times faster in these background threads (in the time to execute them).</p>
  
  <p>Why might this be? Is it the threading libraries I'm using? Surely there shouldn't be such a difference? Or could it be the runtime libraries underneath it all?</p>
  
  <p>Here are my machine details</p>
  
  <p>Mac:</p>
  
  <p>Intel(R) Core(TM) i7-3820QM CPU @ 2.70GHz</p>
  
  <p>16 GB DDR3 1600 MHz</p>
  
  <p>Model MacBookPro9,1</p>
  
  <p>OS: Mac OSX 10.8.5</p>
  
  <p>Windows:</p>
  
  <p>Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz</p>
  
  <p>16 GB DDR3 1600 MHz</p>
  
  <p>Model: Dell Latitude E5530</p>
  
  <p>OS: Windows 7 Service Pack 1</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>To just do a basic speed comparison I compiled this on both machines running from the command line</p>
  
  <pre><code>int main(int agrc, char **argv)
  {
  
      time_t t = time(NULL);
      tm* tt=localtime(&amp;t);
      std::stringstream s;
      s&lt;&lt; std::setfill('0')&lt;&lt;std::setw(2)&lt;&lt;tt-&gt;tm_mday&lt;&lt;"/"&lt;&lt;std::setw(2)&lt;&lt;tt-&gt;tm_mon+1&lt;&lt;"/"&lt;&lt; std::setw(4)&lt;&lt;tt-&gt;tm_year+1900&lt;&lt;" "&lt;&lt; std::setw(2)&lt;&lt;tt-&gt;tm_hour&lt;&lt;":"&lt;&lt;std::setw(2)&lt;&lt;tt-&gt;tm_min&lt;&lt;":"&lt;&lt;std::setw(2)&lt;&lt;tt-&gt;tm_sec;      
  
      std::cout&lt;&lt;"1: "&lt;&lt;s.str()&lt;&lt;std::endl;
  
      double sum=0;
  
      for (int i=0;i&lt;100000000;i++){
          double ii=i*0.123456789;
          sum=sum+sin(ii)*cos(ii);
      }
  
  
      t = time(NULL);
      tt=localtime(&amp;t);
      s.str("");
      s&lt;&lt; std::setfill('0')&lt;&lt;std::setw(2)&lt;&lt;tt-&gt;tm_mday&lt;&lt;"/"&lt;&lt;std::setw(2)&lt;&lt;tt-&gt;tm_mon+1&lt;&lt;"/"&lt;&lt; std::setw(4)&lt;&lt;tt-&gt;tm_year+1900&lt;&lt;" "&lt;&lt; std::setw(2)&lt;&lt;tt-&gt;tm_hour&lt;&lt;":"&lt;&lt;std::setw(2)&lt;&lt;tt-&gt;tm_min&lt;&lt;":"&lt;&lt;std::setw(2)&lt;&lt;tt-&gt;tm_sec;
  
      std::cout&lt;&lt;"2: "&lt;&lt;s.str()&lt;&lt;std::endl;
  
  
  }
  </code></pre>
  
  <p>Windows takes less than a second. Mac takes 4/5 seconds. Any ideas?</p>
  
  <p>On Mac I'm compiling with g++, with visual studio 2013 on windows.</p>
  
  <p><strong>SECOND EDIT</strong></p>
  
  <p>if I change the line</p>
  
  <pre><code>std::cout&lt;&lt;"2: "&lt;&lt;s.str()&lt;&lt;std::endl;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>std::cout&lt;&lt;"2: "&lt;&lt;s.str()&lt;&lt;" "&lt;&lt;sum&lt;&lt;std::endl;
  </code></pre>
  
  <p>Then all of a sudden Windows takes a little bit longer...</p>
  
  <p>This makes me think that the whole thing might be compiler optimisation. So the question would be is g++ (4.2 is the version I have) worse at optimisation or do I need to provide additional flags?</p>
  
  <p><strong>THIRD(!) AND FINAL EDIT</strong></p>
  
  <p>I can report that I achieve comparable performance by ensuring g++ optimisation flags <code>-O</code> were provided at compile time. One of those annoying things that happens so often</p>
  
  <p>A: Im tearing my hair out on problem x</p>
  
  <p>B: Are you sure you're not doing y?</p>
  
  <p>A: That works, why is this information not plastered all over the place and in every tutorial on problem x on the web?</p>
  
  <p>B: Did you read the manual?</p>
  
  <p>A: No, if I completely read the manual for every single bit of code/program I used I would never actually get round to doing anything...</p>
  
  <p>Meh.</p>