ThreadBase.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef AVPLAYER2_THREADBASE_H
  2. #define AVPLAYER2_THREADBASE_H
  3. #include <atomic>
  4. #include <chrono>
  5. #include <condition_variable>
  6. #include <functional>
  7. #include <mutex>
  8. #include <thread>
  9. class ThreadBase
  10. {
  11. public:
  12. ThreadBase()
  13. : m_exit(std::make_shared<std::atomic<bool>>(false))
  14. , m_running(false)
  15. {}
  16. virtual ~ThreadBase()
  17. {
  18. if (m_running.load()) {
  19. // 原子化停止请求
  20. requestExit();
  21. // 安全等待线程结束
  22. safeJoin();
  23. }
  24. }
  25. // 禁止拷贝
  26. ThreadBase(const ThreadBase&) = delete;
  27. ThreadBase& operator=(const ThreadBase&) = delete;
  28. ThreadBase(ThreadBase&&) = delete;
  29. ThreadBase& operator=(ThreadBase&&) = delete;
  30. // 启动线程
  31. void start()
  32. {
  33. if (m_running)
  34. return;
  35. *m_exit = false;
  36. m_running = true;
  37. m_thread = std::thread(&ThreadBase::threadEntry, this);
  38. }
  39. // 请求线程退出
  40. virtual void stop()
  41. {
  42. *m_exit = true;
  43. m_cv.notify_all();
  44. }
  45. // 等待线程退出
  46. void join()
  47. {
  48. if (!m_thread.joinable() || m_joined)
  49. return;
  50. if (std::this_thread::get_id() != m_thread.get_id()) {
  51. m_thread.join();
  52. m_joined = true;
  53. m_running = false;
  54. }
  55. }
  56. bool isRunning() const { return m_running.load(); }
  57. bool isExit() const { return m_exit && m_exit->load(); }
  58. // 设置线程结束回调
  59. void setOnFinished(std::function<void()> cb)
  60. {
  61. // 使用弱引用避免悬空指针
  62. auto weakExit = std::weak_ptr(m_exit);
  63. m_onFinished = [weakExit, cb]() {
  64. if (auto exitPtr = weakExit.lock()) {
  65. // 仅当退出标志设置后执行回调
  66. if (*exitPtr) {
  67. cb();
  68. }
  69. }
  70. };
  71. }
  72. protected:
  73. virtual void run() = 0;
  74. std::shared_ptr<std::atomic<bool>> m_exit;
  75. std::atomic<bool> m_running;
  76. std::thread m_thread;
  77. std::condition_variable m_cv;
  78. std::mutex m_mutex;
  79. private:
  80. // 新增辅助变量用于安全退出检测
  81. std::atomic<bool> m_finishFlag{false};
  82. std::mutex m_finishMutex;
  83. std::condition_variable m_finishCv;
  84. bool m_joined = false;
  85. std::function<void()> m_onFinished;
  86. // 安全的退出请求
  87. void requestExit() noexcept
  88. {
  89. if (m_exit) {
  90. m_exit->store(true);
  91. m_cv.notify_all();
  92. }
  93. }
  94. // 使用条件变量实现安全的超时等待
  95. void safeJoin() noexcept
  96. {
  97. if (!m_thread.joinable())
  98. return;
  99. // 检查是否在自身线程中
  100. if (std::this_thread::get_id() == m_thread.get_id()) {
  101. m_thread.detach();
  102. m_joined = true;
  103. return;
  104. }
  105. // 超时等待线程结束
  106. {
  107. std::unique_lock<std::mutex> lock(m_finishMutex);
  108. if (m_finishCv.wait_for(lock, std::chrono::milliseconds(500), [this] {
  109. return m_finishFlag.load();
  110. })) {
  111. // 线程已正常结束
  112. join();
  113. }
  114. }
  115. // 最终资源释放
  116. if (m_thread.joinable()) {
  117. try {
  118. m_thread.join();
  119. } catch (...) {
  120. // 忽略所有异常
  121. }
  122. m_joined = true;
  123. }
  124. }
  125. // 线程入口点
  126. void threadEntry() noexcept
  127. {
  128. try {
  129. run(); // 用户逻辑
  130. } catch (const std::exception& e) {
  131. // 记录标准异常
  132. // 注意:这里不能使用Qt的日志系统,因为可能在非主线程中
  133. // 可以考虑使用线程安全的日志记录方式
  134. (void)e; // 避免未使用变量警告
  135. } catch (...) {
  136. // 记录未知异常
  137. // 同样需要线程安全的日志记录
  138. }
  139. // 确保退出标志被设置
  140. if (m_exit) {
  141. *m_exit = true;
  142. }
  143. // 设置完成标志
  144. m_finishFlag = true;
  145. m_running = false;
  146. m_finishCv.notify_one();
  147. // 安全执行回调
  148. try {
  149. if (m_onFinished) {
  150. m_onFinished();
  151. }
  152. } catch (...) {
  153. // 回调函数异常不应影响线程清理
  154. }
  155. }
  156. };
  157. #endif // AVPLAYER2_THREADBASE_H