TaskCenter.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2022-04-07 17:19:16
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-04-15 11:26:20
  8. */
  9. #ifndef __TASHCENTER_HPP_
  10. #define __TASHCENTER_HPP_
  11. #include "ins_base.hpp"
  12. #include <condition_variable>
  13. #include <thread>
  14. #include <queue>
  15. #include <vector>
  16. #include <mutex>
  17. using namespace std;
  18. namespace ins
  19. {
  20. class TaskCenter:public Module
  21. {
  22. private:
  23. TaskCenter();
  24. public:
  25. using Ptr = std::shared_ptr<TaskCenter>;
  26. ~TaskCenter(){this->Destroy();}
  27. /**
  28. * @description: 获取实例
  29. * @param {*}
  30. * @return {*}
  31. */
  32. static std::shared_ptr<TaskCenter> getPtr();
  33. /**
  34. * @description: 创建实例
  35. * @param {*}
  36. * @return {*}
  37. */
  38. static std::shared_ptr<TaskCenter> CreateNew();
  39. /**
  40. * @description: 初始化
  41. * @param {*}
  42. * @return {*}
  43. */
  44. int8_t Init() override;
  45. /**
  46. * @description: 释放资源
  47. * @param {*}
  48. * @return {*}
  49. */
  50. void Destroy() override;
  51. /**
  52. * @description: 是否正常
  53. * @param {*}
  54. * @return {*}
  55. */
  56. bool isNormally() override{
  57. return true;
  58. }
  59. /**
  60. * @description: 任务入队
  61. * @param {Ptr&} task 任务
  62. * @return {*}
  63. */
  64. int8_t enqueue(ins::InsTask::Ptr& task);
  65. /**
  66. * @description: 处理数据包
  67. * @param {*}
  68. * @return {*}
  69. */
  70. int8_t ProcessingPacket(InsPacket::Ptr& pack);
  71. private:
  72. queue<ins::InsTask::Ptr> async_Tasks;
  73. vector<thread> workers;
  74. condition_variable condition;
  75. mutex m_mutex;
  76. bool stop = false;
  77. };
  78. } // namespace ins
  79. #endif