Update Timer class to accept functions with any number of parameters
This commit is contained in:
+10
-14
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user