WorkThreadPool.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/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 "Poller/EventPoller.h"
  14. namespace toolkit {
  15. class WorkThreadPool : public std::enable_shared_from_this<WorkThreadPool>, public TaskExecutorGetterImp {
  16. public:
  17. using Ptr = std::shared_ptr<WorkThreadPool>;
  18. ~WorkThreadPool() override = default;
  19. /**
  20. * 获取单例
  21. */
  22. static WorkThreadPool &Instance();
  23. /**
  24. * 设置EventPoller个数,在WorkThreadPool单例创建前有效
  25. * 在不调用此方法的情况下,默认创建thread::hardware_concurrency()个EventPoller实例
  26. * @param size EventPoller个数,如果为0则为thread::hardware_concurrency()
  27. */
  28. static void setPoolSize(size_t size = 0);
  29. /**
  30. * 内部创建线程是否设置cpu亲和性,默认设置cpu亲和性
  31. */
  32. static void enableCpuAffinity(bool enable);
  33. /**
  34. * 获取第一个实例
  35. * @return
  36. */
  37. EventPoller::Ptr getFirstPoller();
  38. /**
  39. * 根据负载情况获取轻负载的实例
  40. * 如果优先返回当前线程,那么会返回当前线程
  41. * 返回当前线程的目的是为了提高线程安全性
  42. * @return
  43. */
  44. EventPoller::Ptr getPoller();
  45. protected:
  46. WorkThreadPool();
  47. };
  48. } /* namespace toolkit */
  49. #endif /* UTIL_WORKTHREADPOOL_H_ */