BaseCalculateThread.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "BaseCalculateThread.h"
  2. BaseCalculateThread::BaseCalculateThread(CalculateThreadInfo_t& threadInfo)
  3. : m_threadInfo(threadInfo)
  4. {
  5. m_logger = spdlog::get("Calculate");
  6. if (m_logger == nullptr)
  7. {
  8. fmt::print("BaseCalculateThread: Calculate Logger not found.\n");
  9. return;
  10. }
  11. }
  12. /**
  13. * @brief 线程任务函数,创建线程时会调用此函数
  14. */
  15. void BaseCalculateThread::threadTask()
  16. {
  17. m_logBase = fmt::format("对比项: {} 线程类型: {}",
  18. m_threadInfo.compareItemInfo.strName.toStdString(),
  19. static_cast<int>(m_threadInfo.threadType));
  20. m_threadInfo.threadState = EThreadState::State_Running;
  21. m_isStop = false;
  22. /* 执行任务,并阻塞到这个函数中,直到任务退出 */
  23. task();
  24. SPDLOG_LOGGER_INFO(m_logger, "{} 执行结束", m_logBase);
  25. m_isRunning = false;
  26. /* 清理资源 */
  27. /* 设置全局的线程状态 */
  28. m_threadInfo.threadState = EThreadState::State_Stopped;
  29. m_isStop.store(true);
  30. }
  31. /* 更新线程信息 */
  32. void BaseCalculateThread::updateThreadInfo(const CalculateThreadInfo_t& threadInfo)
  33. {
  34. if(threadInfo.compareItemInfo.nID != m_threadInfo.compareItemInfo.nID)
  35. {
  36. SPDLOG_LOGGER_WARN(m_logger, "新传入的对比项ID {} 和当前线程的对比项ID {} 不匹配,无法更新线程信息",
  37. threadInfo.compareItemInfo.nID, m_threadInfo.compareItemInfo.nID);
  38. }
  39. m_threadInfoNew = threadInfo;
  40. m_isUpdate = true;
  41. }
  42. /* 停止线程,只设置个停止标志,不阻塞等待 */
  43. void BaseCalculateThread::stopThread()
  44. {
  45. m_isRunning = false;
  46. SPDLOG_LOGGER_INFO(m_logger, "{} 线程设置为停止状态", m_logBase);
  47. }
  48. /* 停止线程 */
  49. void BaseCalculateThread::stopThreadBlock()
  50. {
  51. m_isRunning = false;
  52. while(m_isStop.load() == false)
  53. {
  54. /* 100us检查一次 */
  55. std::this_thread::sleep_for(std::chrono::microseconds(100));
  56. }
  57. }
  58. /* 更新线程信息 */
  59. bool BaseCalculateThread::updateThreadInfoInternal()
  60. {
  61. if(!m_isUpdate)
  62. {
  63. return false;
  64. }
  65. /* 只更新对比项的信息 */
  66. m_threadInfo.compareItemInfo = m_threadInfoNew.compareItemInfo;
  67. m_isUpdate = false;
  68. // SPDLOG_LOGGER_INFO(m_logger, "{} 更新线程信息", m_logBase);
  69. return true;
  70. }