ThreadPool.h 807 B

123456789101112131415161718192021222324252627282930313233343536
  1. #ifndef THREAD_THREADPOOL_H
  2. #define THREAD_THREADPOOL_H
  3. #include <list>
  4. #include <boost/thread.hpp>
  5. #include <boost/function.hpp>
  6. #include <boost/noncopyable.hpp>
  7. #include <boost/detail/atomic_count.hpp>
  8. #include "Thread/ThreadTypedef.h"
  9. #include "Base/Marcos.h"
  10. namespace Egametang {
  11. class ThreadPool: private boost::noncopyable
  12. {
  13. private:
  14. int thread_num;
  15. boost::detail::atomic_count work_num;
  16. volatile bool running;
  17. boost::mutex mutex;
  18. boost::condition_variable cond;
  19. boost::condition_variable done;
  20. std::list<ThreadPtr> threads;
  21. std::list<boost::function<void (void)> > tasks;
  22. void Runner();
  23. public:
  24. ThreadPool(int num = 0);
  25. virtual ~ThreadPool();
  26. virtual void Wait();
  27. virtual bool Schedule(boost::function<void (void)> task);
  28. };
  29. } // namespace Egametang
  30. #endif // THREAD_THREADPOOL_H