Update Timer class to accept functions with any number of parameters

This commit is contained in:
2019-01-05 13:27:47 +01:00
parent 7ad040c6c1
commit 4c66aa0831
6 changed files with 77 additions and 17 deletions
+10 -14
View File
@@ -1,4 +1,5 @@
#include <chrono>
#include "Util.h"
#include "Timer.h"
@@ -6,29 +7,24 @@ namespace PresenceDetection {
namespace Util {
Timer::Timer() :
m_run(false)
m_run(false),
m_identifier(CreateRandomString(10))
{
}
Timer::~Timer()
{
Stop();
Wait();
}
void Timer::Start(int interval, std::function<void(void)> function)
bool Timer::operator==(const Timer& other) const
{
if (m_run.load(std::memory_order_acquire))
Stop();
return m_identifier == other.Identifier();
}
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));
}
});
std::string Timer::Identifier() const
{
return m_identifier;
}
void Timer::Stop()