ThreadPool.h 804 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 <boost/detail/atomic_count.hpp>
  8. #include "Base/Base.h"
  9. namespace Hainan {
  10. class ThreadPool: private boost::noncopyable
  11. {
  12. private:
  13. int thread_num;
  14. boost::detail::atomic_count work_num;
  15. volatile bool running;
  16. boost::mutex mutex;
  17. boost::condition_variable cond;
  18. boost::condition_variable done;
  19. std::list<ThreadPtr> threads;
  20. std::list<boost::function<void (void)> > tasks;
  21. void Runner();
  22. public:
  23. ThreadPool(int num = 0);
  24. ~ThreadPool();
  25. void Start();
  26. void Stop();
  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