title: Boost thread Overhead
body: <p>I found that boost thread overhead has three order of magnitude timing overhead in the following simple program. Is there anyway to reduce this overhead and speedup the <code>fooThread()</code> call ?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;time.h&gt;
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/date_time.hpp&gt;
  typedef uint64_t tick_t;
  #define rdtscll(val) do { \
      unsigned int __a,__d; \
      __asm__ __volatile__("rdtsc" : "=a" (__a), "=d" (__d)); \
          (val) = ((unsigned long long)__a) | (((unsigned long long)__d)&lt;&lt;32); \
      } while(0)
  
  
  class baseClass {
   public:
     void foo(){
               //Do nothing 
          }
         void threadFoo(){
            threadObjOne = boost::thread(&amp;baseClass::foo, this);
                threadObjOne.join();
     }
  
   private:
     boost::thread threadObjOne;
   };
  
  int main(){
     std::cout&lt;&lt; "main startup"&lt;&lt;std::endl; 
     baseClass baseObj; 
     tick_t startTime,endTime;
         rdtscll(startTime);
     baseObj.foo();
     rdtscll(endTime);
     std::cout&lt;&lt;"native foo() call takes "&lt;&lt; endTime-startTime &lt;&lt;" clock cycles"&lt;&lt;std::endl;
     rdtscll(startTime);
     baseObj.threadFoo();
         rdtscll(endTime);
         std::cout&lt;&lt;"Thread foo() call takes "&lt;&lt; endTime-startTime &lt;&lt;" clock cycles"&lt;&lt;std::endl;  
    }
  </code></pre>
  
  <p>You can compile it with <code>g++ -lboost_thread-mt main.cpp</code> and here is the sample output in my machine:</p>
  
  <pre><code>main startup
  native foo() call takes 2187 clock cycles
  Thread foo() call takes 29630434 clock cycles
  </code></pre>