Added task timer to namespace and created a task delayer class

main
anulax1225 ago%!(EXTRA string=1 year)
parent e0aac447e8
commit fdeff2d7d1
  1. 29
      src/bakatools/thread/task_delayer.h
  2. 10
      src/bakatools/thread/task_timer.h

@ -0,0 +1,29 @@
#pragma once
#include <spch.h>
#include <time/time.h>
namespace Bk {
template<typename P>
class TaskDelayer
{
public:
TaskDelayer(int n_ts,
std::unique_ptr<std::function<void()>> action
): ts(n_ts)
{
worker = std::thread([this](std::unique_ptr<std::function<void()>> action)
{
std::function<void()>& task = *action;
std::this_thread::sleep_for(ts.interval);
task();
}, std::move(action));
}
~TaskDelayer() { worker.join(); }
private:
std::thread worker;
TimeSpan<P> ts;
};
}

@ -1,15 +1,16 @@
#pragma once
#include <bakatools.h>
#include <bakatools/time/time.h>
namespace Bk::Tools {
#include <spch.h>
#include <time/time.h>
namespace Bk {
template<typename P>
class TaskTimer
{
public:
TaskTimer(TimeSpan<P> ts)
: ts(ts) {}
~TaskTimer() { if (running) stop(); }
~TaskTimer() { stop(); }
void start(std::unique_ptr<std::function<void()>> action)
{
@ -28,6 +29,7 @@ namespace Bk::Tools {
void stop()
{
if (!running) return;
running = false;
worker.join();
}

Loading…
Cancel
Save