BaseCalculateThread.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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::thread_task()
  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_isRunning = true;
  22. m_isStoped = false;
  23. /* 执行任务,并阻塞到这个函数中,直到任务退出 */
  24. task();
  25. m_isRunning = false;
  26. SPDLOG_INFO("★ {} 执行结束", m_logBase);
  27. /* 设置全局的线程状态 */
  28. m_threadInfo.threadState = EThreadState::State_Stopped;
  29. m_isStoped.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_WARN("新传入的对比项ID {} 和当前线程的对比项ID {} 不匹配,无法更新线程信息",
  37. threadInfo.compareItemInfo.nID, m_threadInfo.compareItemInfo.nID);
  38. }
  39. m_threadInfoNew = threadInfo;
  40. m_isUpdate = true;
  41. }
  42. /* 停止线程,只设置个停止标志,不阻塞等待 */
  43. void BaseCalculateThread::thread_stop()
  44. {
  45. m_isRunning = false;
  46. SPDLOG_INFO("{} 线程设置为停止状态", m_logBase);
  47. }
  48. /* 停止线程 */
  49. void BaseCalculateThread::thread_stop_block()
  50. {
  51. m_isRunning = false;
  52. while(m_isStoped.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_INFO(m_logger, "{} 更新线程信息", m_logBase);
  69. return true;
  70. }