title: C++ ThreadPool is not running parallel
body: <p>I've tried to implement a ThreadPool, but unfortunately I'm running into some problems.</p>
  
  <p>This is what I have already.</p>
  
  <pre><code>//includes ...
  
  void call()
  {
      std::cout &lt;&lt; "Hi i'm thread no " &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl;
      std::this_thread::sleep_for(std::chrono::seconds(2));
      std::cout &lt;&lt; "ready " &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl;
  };
  
  //Implementation is not shown here to reduce code
  class WorkQueue {
      public:
          bool push(std::function&lt;void()&gt; const&amp; value);
          void pop();
          bool empty();
  };
  
  std::condition_variable g_queuecheck;
  std::mutex              g_lockqueue;
  std::atomic&lt;bool&gt;       g_notified;
  
  class ThreadPool
  {
      public:
          ThreadPool(int iNoThread) :
              m_noThread(iNoThread)
          {
              g_notified.store(false);
              m_threads.resize(iNoThread);
              bIsReady.store(false);
              for (int i = 0; i &lt; iNoThread; ++i)
                  m_threads[i] = std::thread(&amp;ThreadPool::run, this);
          }
  
          void run()
          {
              while (!bIsReady || !m_workQueue.empty())
              {
                  std::unique_lock&lt;std::mutex&gt; locker(g_lockqueue);
                  if (m_workQueue.empty())
                  {
                      while (!g_notified) // Used to avoid spurious wakeups
                      {
                          g_queuecheck.wait(locker);
                      }
                      if(!bIsReady)
                          g_notified.store(false);
                  }
  
                  m_workQueue.pop();
              }
          };
  
          void addWork(std::function&lt;void()&gt; func)
          {
              m_workQueue.push(func);
              g_notified.store(true);
              g_queuecheck.notify_one();
          }
  
          void join()
          {
              bIsReady.store(true);
              g_notified.store(true);
              g_queuecheck.notify_all();
  
              for (int i = 0; i &lt; m_noThread; ++i)
                  m_threads[i].join();
          }
  
          ~ThreadPool()
          {}
  
  
          WorkQueue m_workQueue;
          int m_noThread;
          std::vector&lt;std::thread&gt; m_threads;
          std::atomic&lt;bool&gt; bIsReady;
  };
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      {
          ThreadPool pool(4);
  
          for (int i = 0; i &lt; 8; ++i)
              pool.addWork(call); //This work is done sequentially
  
          pool.join();
      }
  
      std::cin.ignore();
      return 0;
  }
  </code></pre>
  
  <p>My problem is that the work is done sequentially.</p>
  
  <ol>
  <li>How can I fix this?</li>
  <li>Is something else wrong with my ThreadPool?</li>
  <li>Is the waiting best-practice?</li>
  </ol>