Timer.cpp 892 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <chrono>
  2. #include "Util.h"
  3. #include "Timer.h"
  4. namespace PresenceDetection {
  5. namespace Util {
  6. Timer::Timer() :
  7. m_run(false),
  8. m_aborted(false),
  9. m_identifier(CreateRandomString(10))
  10. {
  11. }
  12. Timer::~Timer()
  13. {
  14. Abort();
  15. Wait();
  16. }
  17. bool Timer::operator==(const Timer& other) const
  18. {
  19. return m_identifier == other.Identifier();
  20. }
  21. std::string Timer::Identifier() const
  22. {
  23. return m_identifier;
  24. }
  25. void Timer::Stop()
  26. {
  27. m_run.store(false, std::memory_order_release);
  28. }
  29. void Timer::Abort()
  30. {
  31. m_run.store(false, std::memory_order_release);
  32. {
  33. std::unique_lock<std::mutex> lock(m_mutex);
  34. m_aborted = true;
  35. }
  36. m_conditionVariable.notify_all();
  37. }
  38. bool Timer::IsRunning() const noexcept
  39. {
  40. return (m_run.load(std::memory_order_acquire) && m_thread.joinable());
  41. }
  42. void Timer::Wait()
  43. {
  44. if (m_thread.joinable())
  45. m_thread.join();
  46. }
  47. } // namespace Util
  48. } // namespace PresenceDetection