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: 246b99d554a8813bb068f69b3d41edc211125b07 (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
#pragma once

#include "base/thread.hpp"

#include <chrono>
#include <condition_variable>
#include <functional>
#include <mutex>

namespace my
{
class DeferredTask
{
  using TDuration = std::chrono::duration<double>;
  threads::SimpleThread m_thread;
  std::mutex m_mutex;
  std::condition_variable m_cv;
  std::function<void()> m_fn;
  TDuration m_duration;
  bool m_terminate = false;

public:
  DeferredTask(TDuration const & duration);
  ~DeferredTask();

  void Drop();

  template <typename TFn>
  void RestartWith(TFn const && fn)
  {
    {
      std::unique_lock<std::mutex> l(m_mutex);
      m_fn = fn;
    }
    m_cv.notify_one();
  }
};
}  // namespace my