ThreadPool.h 763 B

1234567891011121314151617181920212223242526272829303132333435
  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 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. typedef boost::shared_ptr<ThreadPool> ThreadPoolPtr;
  29. } // namespace Hainan
  30. #endif // THREAD_THREAD_POOL_H