Files
PresenceDetection/Util/Timer.cpp
T
2018-12-18 12:07:12 +01:00

52 lines
846 B
C++

#include <chrono>
#include "Timer.h"
namespace PresenceDetection {
namespace Util {
Timer::Timer() :
m_run(false)
{
}
Timer::~Timer()
{
Stop();
}
void Timer::Start(int interval, std::function<void(void)> function)
{
if (m_run.load(std::memory_order_acquire))
Stop();
m_run.store(true, std::memory_order_release);
m_thread = std::thread([this, interval, function]()
{
while (m_run.load(std::memory_order_acquire))
{
function();
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
});
}
void Timer::Stop()
{
m_run.store(false, std::memory_order_release);
}
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