| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #ifndef AV_BASE_MEDIA_THREAD_BASE_H
- #define AV_BASE_MEDIA_THREAD_BASE_H
- #include "types.h"
- #include <atomic>
- #include <thread>
- #include <mutex>
- #include <condition_variable>
- #include <functional>
- #include <memory>
- #include <chrono>
- namespace av {
- // 线程状态枚举
- enum class ThreadState {
- STOPPED,
- STARTING,
- RUNNING,
- STOPPING
- };
- // 线程基类
- class ThreadBase {
- public:
- ThreadBase();
- virtual ~ThreadBase();
-
- // 禁止拷贝和移动
- ThreadBase(const ThreadBase&) = delete;
- ThreadBase& operator=(const ThreadBase&) = delete;
- ThreadBase(ThreadBase&&) = delete;
- ThreadBase& operator=(ThreadBase&&) = delete;
-
- // 线程控制
- bool start();
- void stop();
- void join();
-
- // 状态查询
- bool isRunning() const;
- bool isStopping() const;
- ThreadState getState() const;
-
- // 线程ID
- std::thread::id getThreadId() const;
-
- // 设置线程名称(用于调试)
- void setThreadName(const std::string& name);
- std::string getThreadName() const;
-
- // 回调设置
- void setOnStarted(std::function<void()> callback);
- void setOnFinished(std::function<void()> callback);
- void setOnError(std::function<void(const std::string&)> callback);
-
- // 等待条件
- void waitForCondition(std::function<bool()> condition, int timeoutMs = -1);
- void notifyCondition();
-
- protected:
- // 子类需要实现的运行逻辑
- virtual void run() = 0;
-
- // 辅助方法
- bool shouldExit() const;
- void sleepMs(int ms);
- bool waitForMs(int ms); // 可中断的等待
-
- // 错误处理
- void reportError(const std::string& error);
-
- // 线程同步
- std::mutex m_mutex;
- std::condition_variable m_condition;
-
- private:
- void threadEntry();
- void setState(ThreadState state);
- void cleanup();
-
- // 线程控制
- std::atomic<bool> m_exitRequested{false};
- std::atomic<ThreadState> m_state{ThreadState::STOPPED};
- std::unique_ptr<std::thread> m_thread;
-
- // 线程信息
- std::string m_threadName;
- std::thread::id m_threadId;
-
- // 回调函数
- std::function<void()> m_onStarted;
- std::function<void()> m_onFinished;
- std::function<void(const std::string&)> m_onError;
-
- // 同步控制
- mutable std::mutex m_stateMutex;
- std::condition_variable m_stateCondition;
-
- // 超时控制
- static constexpr int DEFAULT_JOIN_TIMEOUT_MS = 5000;
- };
- // 工作线程模板类(用于简单的循环工作)
- template<typename WorkFunc>
- class WorkerThread : public ThreadBase {
- public:
- explicit WorkerThread(WorkFunc workFunc, int intervalMs = 10)
- : m_workFunc(std::move(workFunc))
- , m_intervalMs(intervalMs) {
- }
-
- void setInterval(int intervalMs) {
- std::lock_guard<std::mutex> lock(m_mutex);
- m_intervalMs = intervalMs;
- }
-
- int getInterval() const {
- std::lock_guard<std::mutex> lock(m_mutex);
- return m_intervalMs;
- }
-
- protected:
- void run() override {
- while (!shouldExit()) {
- try {
- m_workFunc();
- } catch (const std::exception& e) {
- reportError("Work function exception: " + std::string(e.what()));
- } catch (...) {
- reportError("Unknown work function exception");
- }
-
- if (!shouldExit()) {
- waitForMs(m_intervalMs);
- }
- }
- }
-
- private:
- WorkFunc m_workFunc;
- int m_intervalMs;
- };
- // 便捷的工作线程创建函数
- template<typename WorkFunc>
- std::unique_ptr<WorkerThread<WorkFunc>> makeWorkerThread(WorkFunc workFunc, int intervalMs = 10) {
- return std::make_unique<WorkerThread<WorkFunc>>(std::move(workFunc), intervalMs);
- }
- } // namespace av
- #endif // AV_BASE_THREAD_BASE_H
|