Listing 3

class last_call : public simple_call
{
  virtual bool invoke()
  { return false; }
  virtual bool is_completed() 
    const { return false; }
  virtual void wait_completion() {}
  virtual last_call* make_clone() const
  { return new last_call; }
};
class thread_pool
{
public:
  explicit thread_pool(unsigned int 
    threads) 
    : _threads(threads)
  {
    for(unsigned int i = 0; i != _threads; ++i)
      _thread_group.create_thread(&thread_func);
  }
  ~thread_pool()
  {
    try
    {
      work_queue& wq = work_queue::instance();
      for(unsigned int i = 0; i != _threads; ++i)
        wq.queue_item(shared_ptr<simple_call>
          (new last_call));
      _thread_group.join_all();
    }
    catch(...) {}
  }
  static void thread_func()
  {
    work_queue& wq = work_queue::instance();
    bool keep_processing = true;
    while(keep_processing)
    {
      shared_ptr<simple_call> call = wq.dequeue_item();
      try
      {
        keep_processing = call->invoke();
      }
      catch(...) {}
    }
  }
private:
  unsigned int _threads;
  thread_group _thread_group;
};