Threadpool implementation in C++11 I am implementing a very primitive threadpool with C++11 using mutex and conditional variable. The class ThreadPool is the main thread holder and will spawn a number of threads upon construction, and join all threads on destruction. The main thread adds task of type function&lt;void()&gt; into the queue and then wakes up any waiting threads through condition variable.

As a new C++11 learner, is there anything I can improve for the following code (or fix bugs)? For example, making the task function more generic and implementing the lock-free mechanism (best practice?).

#include &lt;vector&gt;
#include &lt;queue&gt;
#include &lt;memory&gt;
#include &lt;thread&gt;
#include &lt;mutex&gt;
#include &lt;condition_variable&gt;
#include &lt;future&gt;
#include &lt;functional&gt;
#include &lt;iostream&gt;
#include &lt;stdexcept&gt;
#include &lt;unistd.h&gt;

// Class: ThreadPool
class ThreadPool {

  // Class: Worker
  class Worker {

    public:

    // Constructor.
    explicit Worker(ThreadPool* master) : _master{master} {};

    // Functor.
    void operator ()() {

      ::std::function&lt;void()&gt; task;

      while(true) {
        { // Acquire lock.
          ::std::unique_lock&lt;::std::mutex&gt; lock(_master-&gt;_mutex);

          _master-&gt;_condition.wait(
            lock,
            [&amp;] () { return !_master-&gt;_tasks.empty() || _master-&gt;_terminate.load(); }
          );      

          if(_master-&gt;_terminate.load()) return;

          task = _master-&gt;_tasks.front();
          _master-&gt;_tasks.pop_front();
        } // Release lock.

        task();
      }
    }

    private:

    ThreadPool* _master;
  };

  public:

    ThreadPool(size_t);
    template&lt;class F&gt;
    void enqueue(F f);
    ~ThreadPool();

    ThreadPool(const ThreadPool&amp;) = delete;
    ThreadPool&amp; operator = (const ThreadPool&amp;) = delete;

  private:

    friend class Worker;

    ::std::vector&lt; ::std::thread &gt; _workers;
    ::std::deque&lt; ::std::function&lt;void()&gt; &gt; _tasks;
    ::std::mutex _mutex;
    ::std::condition_variable _condition;
    ::std::atomic_bool _terminate {false};
};

// Constructor
ThreadPool::ThreadPool(size_t threads) {
  for(size_t i = 0;i&lt;threads;++i) {
    _workers.push_back(::std::thread(Worker(this)));
  }
}

// Destructor.
ThreadPool::~ThreadPool() {
  _terminate.store(true);
  _condition.notify_all();
  for(auto&amp; t : _workers) t.join();
}

// Procedure: enqueue
template&lt;class F&gt;
void ThreadPool::enqueue(F f) {

  { // Acquire lock
    ::std::unique_lock&lt;::std::mutex&gt; guard(_mutex);
    _tasks.push_back(::std::function&lt;void()&gt;(f));
  } // Release lock

  // wake up one thread
  _condition.notify_one();
}

void foo() {
  printf("hello by %p\n", ::std::this_thread::get_id());
  sleep(1); 
}

int main() {


  ThreadPool tp(4);

  while(1) {

    printf("main %p: enque\n", ::std::this_thread::get_id());
    tp.enqueue(foo);
    sleep(1);
  }

  return 0;
}
I don't see why this should have a hypothetical name:


void foo() {
    printf("hello by %p\n", ::std::this_thread::get_id());
    sleep(1); 
}



If this is meant as a test function, then name it as such.  As obvious as the functionality may be, there's still need for such names in an otherwise serious program.

Also, you can use std::this_thread::sleep_for, which is as of C++11:

std::this_thread::sleep_for(std::chrono::seconds(1));


If you also have C++14, use a literal instead:

std::this_thread::sleep_for(1s);


(You'll also need &lt;chrono&gt; for all of this.)