Timer.cpp 683 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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_identifier(CreateRandomString(10))
  9. {
  10. }
  11. Timer::~Timer()
  12. {
  13. Wait();
  14. }
  15. bool Timer::operator==(const Timer& other) const
  16. {
  17. return m_identifier == other.Identifier();
  18. }
  19. std::string Timer::Identifier() const
  20. {
  21. return m_identifier;
  22. }
  23. void Timer::Stop()
  24. {
  25. m_run.store(false, std::memory_order_release);
  26. }
  27. bool Timer::IsRunning() const noexcept
  28. {
  29. return (m_run.load(std::memory_order_acquire) && m_thread.joinable());
  30. }
  31. void Timer::Wait()
  32. {
  33. if (m_thread.joinable())
  34. m_thread.join();
  35. }
  36. } // namespace Util
  37. } // namespace PresenceDetection