12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include "BaseCalculateThread.h"
- BaseCalculateThread::BaseCalculateThread(CalculateThreadInfo_t& threadInfo)
- : m_threadInfo(threadInfo)
- {
- m_logger = spdlog::get("Calculate");
- if (m_logger == nullptr)
- {
- fmt::print("BaseCalculateThread: Calculate Logger not found.\n");
- return;
- }
- }
- /**
- * @brief 线程任务函数,创建线程时会调用此函数
- */
- void BaseCalculateThread::threadTask()
- {
- m_logBase = fmt::format("对比项: {} 线程类型: {}",
- m_threadInfo.compareItemInfo.strName.toStdString(),
- static_cast<int>(m_threadInfo.threadType));
- m_threadInfo.threadState = EThreadState::State_Running;
- m_isRunning = true;
- m_isStop = false;
- /* 执行任务,并阻塞到这个函数中,直到任务退出 */
- task();
- m_isRunning = false;
- /* 清理资源 */
- /* 设置全局的线程状态 */
- m_threadInfo.threadState = EThreadState::State_Stopped;
- m_isStop.store(true);
- SPDLOG_LOGGER_INFO(m_logger, "★ {} 执行结束", m_logBase);
- }
- /* 更新线程信息 */
- void BaseCalculateThread::updateThreadInfo(const CalculateThreadInfo_t& threadInfo)
- {
- if(threadInfo.compareItemInfo.nID != m_threadInfo.compareItemInfo.nID)
- {
- SPDLOG_LOGGER_WARN(m_logger, "新传入的对比项ID {} 和当前线程的对比项ID {} 不匹配,无法更新线程信息",
- threadInfo.compareItemInfo.nID, m_threadInfo.compareItemInfo.nID);
- }
- m_threadInfoNew = threadInfo;
- m_isUpdate = true;
- }
- /* 停止线程,只设置个停止标志,不阻塞等待 */
- void BaseCalculateThread::stopThread()
- {
- m_isRunning = false;
- SPDLOG_LOGGER_INFO(m_logger, "{} 线程设置为停止状态", m_logBase);
- }
- /* 停止线程 */
- void BaseCalculateThread::stopThreadBlock()
- {
- m_isRunning = false;
- while(m_isStop.load() == false)
- {
- /* 100us检查一次 */
- std::this_thread::sleep_for(std::chrono::microseconds(100));
- }
- }
- /* 更新线程信息 */
- bool BaseCalculateThread::updateThreadInfoInternal()
- {
- if(!m_isUpdate)
- {
- return false;
- }
- /* 只更新对比项的信息 */
- m_threadInfo.compareItemInfo = m_threadInfoNew.compareItemInfo;
- m_isUpdate = false;
- // SPDLOG_LOGGER_INFO(m_logger, "{} 更新线程信息", m_logBase);
- return true;
- }
|