mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-19 13:03:36 +00:00
committed by
Konstantin Pastbin
parent
c9cbb64f12
commit
76ffc99abd
40
libs/base/deferred_task.hpp
Normal file
40
libs/base/deferred_task.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include "base/thread.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
||||
namespace base
|
||||
{
|
||||
class DeferredTask
|
||||
{
|
||||
public:
|
||||
using Duration = std::chrono::duration<double>;
|
||||
|
||||
explicit DeferredTask(Duration const & duration);
|
||||
~DeferredTask();
|
||||
|
||||
void Drop();
|
||||
|
||||
template <typename Fn>
|
||||
void RestartWith(Fn const && fn)
|
||||
{
|
||||
{
|
||||
std::unique_lock l(m_mutex);
|
||||
m_fn = fn;
|
||||
}
|
||||
m_cv.notify_one();
|
||||
}
|
||||
|
||||
private:
|
||||
threads::SimpleThread m_thread;
|
||||
std::mutex m_mutex;
|
||||
std::condition_variable m_cv;
|
||||
std::function<void()> m_fn;
|
||||
Duration m_duration;
|
||||
bool m_terminate = false;
|
||||
};
|
||||
} // namespace base
|
||||
Reference in New Issue
Block a user