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: de9b9e40cc073020e34e67c3636fae2a39e65d7c (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 "std/chrono.hpp"
#include "std/condition_variable.hpp"
#include "std/function.hpp"
#include "std/mutex.hpp"

namespace my
{
class DeferredTask
{
  using TDuration = duration<double>;
  threads::SimpleThread m_thread;
  mutex m_mutex;
  condition_variable m_cv;
  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)
  {
    {
      unique_lock<mutex> l(m_mutex);
      m_fn = fn;
    }
    m_cv.notify_one();
  }
};
}  // namespace my