39 lines
621 B
C++
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
|