ThreadPool.h 830 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/detail/atomic_count.hpp>
  7. #include "Base/Base.h"
  8. #include "Thread/ThreadPoolIf.h"
  9. namespace Egametang {
  10. class ThreadPool: public ThreadPoolIf, 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. } // namespace Egametang
  30. #endif // THREAD_THREAD_POOL_H