ThreadPool.h 753 B

123456789101112131415161718192021222324252627282930313233343536
  1. #ifndef THREAD_THREAD_POOL_H
  2. #define THREAD_THREAD_POOL_H
  3. #include <list>
  4. #include <boost/thread.hpp>
  5. #include <boost/function.hpp>
  6. #include <boost/bind.hpp>
  7. #include "base/base.h"
  8. namespace Hainan {
  9. class ThreadPool: private boost::noncopyable
  10. {
  11. private:
  12. int num;
  13. volatile int work_num;
  14. volatile bool running;
  15. boost::mutex mutex;
  16. boost::condition_variable cond;
  17. boost::condition_variable done;
  18. std::list<ThreadPtr> threads;
  19. std::list<boost::function<void (void)> > tasks;
  20. void Runner();
  21. public:
  22. ThreadPool();
  23. ~ThreadPool();
  24. void Start();
  25. void Stop();
  26. void SetNum(int num);
  27. bool PushTask(boost::function<void (void)> task);
  28. };
  29. typedef boost::shared_ptr<ThreadPool> ThreadPoolPtr;
  30. } // namespace Hainan
  31. #endif // THREAD_THREAD_POOL_H