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

worker_thread.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e8b7b374b95f680135992833a09f628d5ac6583 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once

#include "base/assert.hpp"
#include "base/thread.hpp"
#include "base/thread_checker.hpp"

#include <condition_variable>
#include <mutex>
#include <queue>
#include <utility>

namespace base
{
// This class represents a simple worker thread with a queue of tasks.
//
// *NOTE* This class IS thread-safe, but it must be destroyed on the
// same thread it was created.
class WorkerThread
{
public:
  enum class Exit
  {
    ExecPending,
    SkipPending
  };

  using Task = std::function<void()>;

  WorkerThread();
  ~WorkerThread();

  // Pushes task to the end of the thread's queue. Returns false when
  // the thread is shut down.
  template <typename T>
  bool Push(T && t)
  {
    std::lock_guard<std::mutex> lk(m_mu);
    if (m_shutdown)
      return false;
    m_queue.emplace(std::forward<T>(t));
    m_cv.notify_one();
    return true;
  }

  // Sends a signal to the thread to shut down. Returns false when the
  // thread was shut down previously.
  bool Shutdown(Exit e);

private:
  void ProcessTasks();

  threads::SimpleThread m_thread;
  std::mutex m_mu;
  std::condition_variable m_cv;

  bool m_shutdown = false;
  Exit m_exit = Exit::SkipPending;

  std::queue<Task> m_queue;

  ThreadChecker m_checker;
};
}  // namespace base