Files
Timer/Timer/Timer.cpp
T

58 lines
841 B
C++

#include "Timer.h"
#include <Random.h>
#include <chrono>
namespace Timer {
Timer::Timer() :
m_run(false),
m_aborted(false),
m_identifier(Random::CreateRandomString(10))
{
}
Timer::~Timer()
{
Abort();
Wait();
}
bool Timer::operator==(const Timer& other) const
{
return m_identifier == other.Identifier();
}
std::string Timer::Identifier() const
{
return m_identifier;
}
void Timer::Stop()
{
m_run.store(false, std::memory_order_release);
}
void Timer::Abort()
{
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 Timer