This repository has been archived on 2022-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
fortuna/do_task.cpp
surtur 4b216a6f6e
All checks were successful
continuous-integration/drone/push Build is passing
refactor: clang-format
2022-01-10 04:25:03 +01:00

39 lines
621 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();
th.join();
}
auto DoTask::thread_pls(const std::chrono::seconds& interval,
std::function<void()> callback) -> void {
if (th.joinable()) {
die_pls();
}
do_sleep.lock();
th = std::thread([this, interval, callback = std::move(callback)] {
while (!do_sleep.try_lock_for(interval)) {
callback();
}
});
}
DoTask::~DoTask() noexcept {
if (th.joinable()) {
die_pls();
}
}
} // namespace fortuna
#endif // FORTUNA_DO_TASK_CPP