media_thread_base.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef AV_BASE_MEDIA_THREAD_BASE_H
  2. #define AV_BASE_MEDIA_THREAD_BASE_H
  3. #include "types.h"
  4. #include <atomic>
  5. #include <thread>
  6. #include <mutex>
  7. #include <condition_variable>
  8. #include <functional>
  9. #include <memory>
  10. #include <chrono>
  11. namespace av {
  12. // 线程状态枚举
  13. enum class ThreadState {
  14. STOPPED,
  15. STARTING,
  16. RUNNING,
  17. STOPPING
  18. };
  19. // 线程基类
  20. class ThreadBase {
  21. public:
  22. ThreadBase();
  23. virtual ~ThreadBase();
  24. // 禁止拷贝和移动
  25. ThreadBase(const ThreadBase&) = delete;
  26. ThreadBase& operator=(const ThreadBase&) = delete;
  27. ThreadBase(ThreadBase&&) = delete;
  28. ThreadBase& operator=(ThreadBase&&) = delete;
  29. // 线程控制
  30. bool start();
  31. void stop();
  32. void join();
  33. // 状态查询
  34. bool isRunning() const;
  35. bool isStopping() const;
  36. ThreadState getState() const;
  37. // 线程ID
  38. std::thread::id getThreadId() const;
  39. // 设置线程名称(用于调试)
  40. void setThreadName(const std::string& name);
  41. std::string getThreadName() const;
  42. // 回调设置
  43. void setOnStarted(std::function<void()> callback);
  44. void setOnFinished(std::function<void()> callback);
  45. void setOnError(std::function<void(const std::string&)> callback);
  46. // 等待条件
  47. void waitForCondition(std::function<bool()> condition, int timeoutMs = -1);
  48. void notifyCondition();
  49. protected:
  50. // 子类需要实现的运行逻辑
  51. virtual void run() = 0;
  52. // 辅助方法
  53. bool shouldExit() const;
  54. void sleepMs(int ms);
  55. bool waitForMs(int ms); // 可中断的等待
  56. // 错误处理
  57. void reportError(const std::string& error);
  58. // 线程同步
  59. std::mutex m_mutex;
  60. std::condition_variable m_condition;
  61. private:
  62. void threadEntry();
  63. void setState(ThreadState state);
  64. void cleanup();
  65. // 线程控制
  66. std::atomic<bool> m_exitRequested{false};
  67. std::atomic<ThreadState> m_state{ThreadState::STOPPED};
  68. std::unique_ptr<std::thread> m_thread;
  69. // 线程信息
  70. std::string m_threadName;
  71. std::thread::id m_threadId;
  72. // 回调函数
  73. std::function<void()> m_onStarted;
  74. std::function<void()> m_onFinished;
  75. std::function<void(const std::string&)> m_onError;
  76. // 同步控制
  77. mutable std::mutex m_stateMutex;
  78. std::condition_variable m_stateCondition;
  79. // 超时控制
  80. static constexpr int DEFAULT_JOIN_TIMEOUT_MS = 5000;
  81. };
  82. // 工作线程模板类(用于简单的循环工作)
  83. template<typename WorkFunc>
  84. class WorkerThread : public ThreadBase {
  85. public:
  86. explicit WorkerThread(WorkFunc workFunc, int intervalMs = 10)
  87. : m_workFunc(std::move(workFunc))
  88. , m_intervalMs(intervalMs) {
  89. }
  90. void setInterval(int intervalMs) {
  91. std::lock_guard<std::mutex> lock(m_mutex);
  92. m_intervalMs = intervalMs;
  93. }
  94. int getInterval() const {
  95. std::lock_guard<std::mutex> lock(m_mutex);
  96. return m_intervalMs;
  97. }
  98. protected:
  99. void run() override {
  100. while (!shouldExit()) {
  101. try {
  102. m_workFunc();
  103. } catch (const std::exception& e) {
  104. reportError("Work function exception: " + std::string(e.what()));
  105. } catch (...) {
  106. reportError("Unknown work function exception");
  107. }
  108. if (!shouldExit()) {
  109. waitForMs(m_intervalMs);
  110. }
  111. }
  112. }
  113. private:
  114. WorkFunc m_workFunc;
  115. int m_intervalMs;
  116. };
  117. // 便捷的工作线程创建函数
  118. template<typename WorkFunc>
  119. std::unique_ptr<WorkerThread<WorkFunc>> makeWorkerThread(WorkFunc workFunc, int intervalMs = 10) {
  120. return std::make_unique<WorkerThread<WorkFunc>>(std::move(workFunc), intervalMs);
  121. }
  122. } // namespace av
  123. #endif // AV_BASE_THREAD_BASE_H