| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #include <iostream>
- #include <chrono>
- #include "Util.h"
- #include "Timer.h"
- namespace PresenceDetection {
- namespace Util {
- Timer::Timer() :
- m_run(false),
- m_aborted(false),
- m_identifier(CreateRandomString(10))
- {
- }
- Timer::~Timer()
- {
- Abort();
- Wait();
- }
- Timer::Timer(Timer&& other) :
- m_run(static_cast<bool>(other.m_run)),
- m_identifier(other.m_identifier),
- m_thread(std::move(other.m_thread))
- {
- }
- Timer& Timer::operator=(Timer&& other)
- {
- m_run = static_cast<bool>(other.m_run);
- m_identifier = std::move(other.m_identifier);
- m_thread = std::move(other.m_thread);
- return *this;
- }
- bool Timer::operator==(const Timer& other) const
- {
- return m_identifier == other.Identifier();
- }
- bool Timer::operator==(const std::string& identifier) const
- {
- return m_identifier == identifier;
- }
- bool Timer::operator==(bool running) const
- {
- return IsRunning() == running;
- }
- std::string Timer::Identifier() const
- {
- return m_identifier;
- }
- void Timer::Stop()
- {
- m_run.store(false, std::memory_order_release);
- }
- void Timer::Abort()
- {
- std::cout << "Timer::Abort()" << std::endl;
- m_run.store(false, std::memory_order_release);
- {
- std::unique_lock<std::mutex> lock(m_mutex);
- m_aborted = true;
- }
- m_conditionVariable.notify_all();
- }
- bool Timer::IsRunning() const noexcept
- {
- return (m_run.load(std::memory_order_acquire) && m_thread.joinable());
- }
- void Timer::Wait()
- {
- if (m_thread.joinable())
- m_thread.join();
- }
- } // namespace Util
- } // namespace PresenceDetection
|