forked from ak-fortuna/fortuna
38 lines
637 B
C++
38 lines
637 B
C++
#ifndef FORTUNA_DO_TASK_CPP
|
|
#define FORTUNA_DO_TASK_CPP
|
|
|
|
#include "do_task.h"
|
|
|
|
#include <utility>
|
|
|
|
namespace fortuna {
|
|
|
|
auto DoTask::die_pls() -> void {
|
|
do_sleep.unlock();
|
|
threadie.join();
|
|
}
|
|
|
|
auto DoTask::thread_pls(const std::chrono::seconds& interval,
|
|
std::function<void()> callback) -> void {
|
|
if (threadie.joinable()) {
|
|
die_pls();
|
|
}
|
|
|
|
do_sleep.lock();
|
|
threadie = std::thread([this, interval, callback = std::move(callback)] {
|
|
while (!do_sleep.try_lock_for(interval)) {
|
|
callback();
|
|
}
|
|
});
|
|
}
|
|
|
|
DoTask::~DoTask() noexcept {
|
|
if (threadie.joinable()) {
|
|
die_pls();
|
|
}
|
|
}
|
|
|
|
} //namespace fortuna
|
|
|
|
#endif//FORTUNA_DO_TASK_CPP
|