67 lines
1.5 KiB
C
67 lines
1.5 KiB
C
|
#pragma once
|
|||
|
|
|||
|
#include "DataMonitor.h"
|
|||
|
#include <thread>
|
|||
|
#include <atomic>
|
|||
|
#include <mutex>
|
|||
|
#include <condition_variable>
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 数据注入线程类,用于持续向数据监控器注入数据
|
|||
|
*/
|
|||
|
class DataInjectThread
|
|||
|
{
|
|||
|
public:
|
|||
|
/**
|
|||
|
* @brief 构造函数
|
|||
|
* @param dataMonitor 数据监控器指针
|
|||
|
* @param data 要注入的数据
|
|||
|
* @param frequency 注入频率(Hz)
|
|||
|
*/
|
|||
|
DataInjectThread(DataMonitorBasePtr dataMonitor,
|
|||
|
std::unordered_map<std::string, std::string> data, double frequency);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 析构函数
|
|||
|
*/
|
|||
|
~DataInjectThread();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 启动数据注入线程
|
|||
|
*/
|
|||
|
void start();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 停止数据注入线程
|
|||
|
*/
|
|||
|
void stop();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 更新要注入的数据
|
|||
|
* @param data 新的数据
|
|||
|
*/
|
|||
|
void updateData(const std::unordered_map<std::string, std::string> &data);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 更新注入频率
|
|||
|
* @param frequency 新的频率(Hz)
|
|||
|
*/
|
|||
|
void updateFrequency(double frequency);
|
|||
|
|
|||
|
private:
|
|||
|
/**
|
|||
|
* @brief 线程执行函数
|
|||
|
*/
|
|||
|
void threadFunc();
|
|||
|
|
|||
|
private:
|
|||
|
std::thread m_thread; ///< 数据注入线程
|
|||
|
std::atomic<bool> m_running; ///< 线程运行标志
|
|||
|
std::mutex m_mutex; ///< 互斥锁
|
|||
|
std::condition_variable m_cv; ///< 条件变量
|
|||
|
DataMonitorBasePtr m_dataMonitor; ///< 数据监控器指针
|
|||
|
std::unordered_map<std::string, std::string> m_data; ///< 要注入的数据
|
|||
|
std::atomic<double> m_frequency; ///< 注入频率
|
|||
|
};
|
|||
|
|
|||
|
using DataInjectThreadPtr = std::shared_ptr<DataInjectThread>;
|