ThreadPool.h 733 B

12345678910111213141516171819202122232425262728293031323334
  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/detail/atomic_count.hpp>
  7. #include "Base/Base.h"
  8. namespace Hainan {
  9. class ThreadPool: private boost::noncopyable
  10. {
  11. private:
  12. int thread_num_;
  13. boost::detail::atomic_count 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(int num = 0);
  23. ~ThreadPool();
  24. void Start();
  25. void Stop();
  26. bool PushTask(boost::function<void (void)> task);
  27. };
  28. } // namespace Hainan
  29. #endif // THREAD_THREAD_POOL_H