title: Sending jobs to a std::thread
body: <p>I am quite new to std::thread and I quickly realized that creating them is quite costly at least on my computer running W7.
  So I decided to create my threads and send jobs to it using that piece of sample code: <a href="http://en.cppreference.com/w/cpp/thread/condition_variable" rel="nofollow">http://en.cppreference.com/w/cpp/thread/condition_variable</a></p>
  
  <p>My code runs well no crash however I didn't notice much performance increase so I measured the difference between the time the job finishes and the time the job is detected finished by the main thread ( see WaitUntilJobFinished() ) I noticed that on some rare occasions the time difference was over 2 milliseconds</p>
  
  <p>Does anyone see anything wrong with the code?</p>
  
  <p>Code:</p>
  
  <pre><code>class CJobParameters
  {
  public:
  };
  
  typedef void (*CJobFunc)( const CJobParameters * );
  
  class CThread
  {   
  public:
      void Start();
      void WaitUntilJobDone();
      void StartJob( CJobFunc inJobFunc, const CJobParameters * inJobParameters );
  
      std::thread m_stdThread;
  
      CJobFunc                m_jobFunc       = nullptr;
      const CJobParameters *  m_jobParameters = nullptr;
      //std::atomic&lt;bool&gt;     m_jobDone       = true;
      std::mutex              m_mutex;
      std::condition_variable m_cv;
  
      __int64 m_jobDoneAt = 0;
      __int64 m_threadJoinedAt = 0;
      __int64 m_lostTime = 0;
  };
  
  class CThreads
  {
  public:
      static void Start();
      static CThread  threadArray[ JOB_COUNT ];
  };
  
  
  void ThreadMain( CThread * inThread )
  {
      while ( true )
      {
          std::unique_lock&lt;std::mutex&gt; lk( inThread-&gt;m_mutex );
          inThread-&gt;m_cv.wait(lk, [ inThread ]{return inThread-&gt;m_jobParameters != nullptr;});
          if ( inThread-&gt;m_jobFunc )
          {
              (*inThread-&gt;m_jobFunc)( inThread-&gt;m_jobParameters );
              inThread-&gt;m_jobFunc = nullptr;
              inThread-&gt;m_jobParameters = nullptr;
              inThread-&gt;m_jobDoneAt = COSToolbox::QuerySystemTime2();
          }
          lk.unlock();
          inThread-&gt;m_cv.notify_one();
          std::this_thread::sleep_for( std::chrono::nanoseconds(0) );
      }
  }
  
  void CThread::StartJob( CJobFunc inJobFunc, const CJobParameters * inJobParameters )
  {
      std::lock_guard&lt;std::mutex&gt; lk( m_mutex );
      m_jobFunc           = inJobFunc;
      m_jobParameters     = inJobParameters;
      m_cv.notify_one();
  }
  
  void CThread::Start()
  {
      m_stdThread = std::thread( ThreadMain, this );
  }
  
  void CThread::WaitUntilJobDone()
  {
      std::unique_lock&lt;std::mutex&gt; lk( m_mutex );
      m_cv.wait(lk, [ this ]{return this-&gt;m_jobParameters == nullptr;});
  
      m_threadJoinedAt = COSToolbox::QuerySystemTime2();
      m_lostTime = m_threadJoinedAt - m_jobDoneAt;
      LOG_INFO( "Thread joined with %f ms lost", (Float32)m_lostTime / 1000 );
  }
  
  
  CThread CThreads::threadArray[ JOB_COUNT ];
  void CThreads::Start()
  {
      for ( Int32 i = 0; i &lt; JOB_COUNT; ++i )
      {
          threadArray[i].Start();
      }
  }
  
  void MyJobFunc( const CJobParameters  * jobParameters )
  {
      // do job here
  }
  void main()
  {
      CThreads::Start();
      while(true)
      {
          CJobParameters jobParametersArray[ JOB_COUNT ];
          for ( Int32 i = 0; i &lt; JOB_COUNT; ++i )
          {
              CThread &amp; thread = CThreads::threadArray[ i ];
              CJobParameters&amp; jobParameters = jobParametersArray[ i ];
              jobParameters.m_ // Fill in params
              thread.StartJob( &amp;MyJobFunc, &amp;jobParameters );
          }
          for ( Int32 i = 0; i &lt; JOB_COUNT; ++i )
          {
              CThread &amp; thread = CThreads::threadArray[ i ];
              // Prints 2 ms sometimes whith i = 0
              thread.WaitUntilJobDone();
          }
       }
  }
  </code></pre>