Implement use of Inventory API
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <chrono>
|
||||
#include "Util.h"
|
||||
#include "Timer.h"
|
||||
@@ -8,12 +10,14 @@ namespace Util {
|
||||
|
||||
Timer::Timer() :
|
||||
m_run(false),
|
||||
m_aborted(false),
|
||||
m_identifier(CreateRandomString(10))
|
||||
{
|
||||
}
|
||||
|
||||
Timer::~Timer()
|
||||
{
|
||||
Abort();
|
||||
Wait();
|
||||
}
|
||||
|
||||
@@ -57,6 +61,17 @@ 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());
|
||||
|
||||
+20
-10
@@ -2,7 +2,9 @@
|
||||
#define UTIL_TIMER_H
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include "Helpers.h"
|
||||
|
||||
@@ -30,18 +32,19 @@ public:
|
||||
std::string Identifier() const;
|
||||
|
||||
template <typename ...FunctionArguments, typename ...Arguments>
|
||||
void StartSingle(int interval, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
void StartSingle(int milliSeconds, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
{
|
||||
Start(TimerType::Single, interval, function, std::forward<Arguments>(arguments)...);
|
||||
Start(TimerType::Single, milliSeconds, function, std::forward<Arguments>(arguments)...);
|
||||
}
|
||||
|
||||
template <typename ...FunctionArguments, typename ...Arguments>
|
||||
void StartContinuous(int interval, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
void StartContinuous(int milliSeconds, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
{
|
||||
Start(TimerType::Continuous, interval, function, std::forward<Arguments>(arguments)...);
|
||||
Start(TimerType::Continuous, milliSeconds, function, std::forward<Arguments>(arguments)...);
|
||||
}
|
||||
|
||||
void Stop();
|
||||
void Abort();
|
||||
bool IsRunning() const noexcept;
|
||||
void Wait();
|
||||
|
||||
@@ -59,7 +62,7 @@ private:
|
||||
|
||||
private:
|
||||
template <typename ...FunctionArguments, typename ...Arguments>
|
||||
void Start(TimerType::type type, int interval, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
void Start(TimerType::type type, int milliSeconds, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
{
|
||||
if (type == TimerType::Unknown)
|
||||
return;
|
||||
@@ -67,17 +70,21 @@ private:
|
||||
if (m_run.load(std::memory_order_acquire))
|
||||
Stop();
|
||||
|
||||
m_thread = std::thread([this, type, interval, function, &arguments...]()
|
||||
m_thread = std::thread([this, type, milliSeconds, function, &arguments...]()
|
||||
{
|
||||
auto argumentsCopy = std::make_tuple(std::forward<Arguments>(arguments)...);
|
||||
m_run.store(true, std::memory_order_release);
|
||||
m_aborted = false;
|
||||
|
||||
while (m_run.load(std::memory_order_acquire))
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
|
||||
Helpers::execute(function, argumentsCopy);
|
||||
if (type == TimerType::Single)
|
||||
m_run.store(false, std::memory_order_release);
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
if (!m_conditionVariable.wait_for(lock, std::chrono::milliseconds(milliSeconds), [&]{return m_aborted;}))
|
||||
{
|
||||
Helpers::execute(function, argumentsCopy);
|
||||
if (type == TimerType::Single)
|
||||
m_run.store(false, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -88,6 +95,9 @@ private:
|
||||
|
||||
private:
|
||||
std::atomic<bool> m_run;
|
||||
bool m_aborted;
|
||||
std::mutex m_mutex;
|
||||
std::condition_variable m_conditionVariable;
|
||||
std::string m_identifier;
|
||||
std::thread m_thread;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user