Welcome to mirror list, hosted at ThFree Co, Russian Federation.

deferred_task.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18dc962a7018a9a91151e982b240db39069d4dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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>;

  DeferredTask(Duration const & duration);
  ~DeferredTask();

  void Drop();

  template <typename Fn>
  void RestartWith(Fn const && fn)
  {
    {
      std::unique_lock<std::mutex> 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