WorkThreadPool.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/xia-chu/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef UTIL_WORKTHREADPOOL_H_
  11. #define UTIL_WORKTHREADPOOL_H_
  12. #include <memory>
  13. #include "ThreadPool.h"
  14. #include "Poller/EventPoller.h"
  15. using namespace std;
  16. namespace toolkit {
  17. class WorkThreadPool :
  18. public std::enable_shared_from_this<WorkThreadPool> ,
  19. public TaskExecutorGetterImp {
  20. public:
  21. typedef std::shared_ptr<WorkThreadPool> Ptr;
  22. ~WorkThreadPool(){};
  23. /**
  24. * 获取单例
  25. * @return
  26. */
  27. static WorkThreadPool &Instance();
  28. /**
  29. * 设置EventPoller个数,在WorkThreadPool单例创建前有效
  30. * 在不调用此方法的情况下,默认创建thread::hardware_concurrency()个EventPoller实例
  31. * @param size EventPoller个数,如果为0则为thread::hardware_concurrency()
  32. */
  33. static void setPoolSize(size_t size = 0);
  34. /**
  35. * 获取第一个实例
  36. * @return
  37. */
  38. EventPoller::Ptr getFirstPoller();
  39. /**
  40. * 根据负载情况获取轻负载的实例
  41. * 如果优先返回当前线程,那么会返回当前线程
  42. * 返回当前线程的目的是为了提高线程安全性
  43. * @return
  44. */
  45. EventPoller::Ptr getPoller();
  46. private:
  47. WorkThreadPool();
  48. };
  49. } /* namespace toolkit */
  50. #endif /* UTIL_WORKTHREADPOOL_H_ */