Kallisto Linux API
Library for interacting with Kallisto devices
executor.hpp
Go to the documentation of this file.
1 
42 #ifndef EXECUTOR_HPP
43 #define EXECUTOR_HPP
44 
45 #include <functional>
46 #include <vector>
47 #include <queue>
48 #include <thread>
49 #include <mutex>
50 #include <atomic>
51 #include <condition_variable>
52 
53 #include "../error/kallisto_error.hpp"
54 
55 namespace kallisto {
56 namespace util {
57 
72 class Executor {
73 public:
77  static const unsigned int MAXIMUM_NUMBER_THREADS;
78 
82  static const unsigned int MINIMUM_NUMBER_THREADS;
83 
87  typedef std::function<void()> ExecutorRunnable;
88 
89 private:
91  std::string m_tag;
93  std::vector<std::thread> m_threads; //Holds the executor threads
95  std::condition_variable m_threads_cv;
96 
98  std::queue<ExecutorRunnable> m_runnable_queue; //Holds the executor requests
100  std::mutex m_runnable_queue_mutex;
101 
103  std::atomic<bool> m_running; //Signals if executor is still accepting tasks.
104 
110  void run(unsigned int i);
111 
115  Executor(Executor const&) = delete;
116  Executor(Executor&&) = delete;
117  void operator=(Executor const&) = delete;
118  void operator=(Executor &&) = delete;
119 
120 public:
127  Executor(std::string tag, unsigned int nr_threads);
128 
132  ~Executor();
133 
139  error::KallistoDetailedResult execute(ExecutorRunnable runnable);
140 
147  error::KallistoDetailedResult shutdown();
148 
155  error::KallistoDetailedResult shutdownNow();
156 
161  void awaitShutdown();
162 };
163 
164 } /* namespace util */
165 } /* namespace kallisto */
166 
167 
168 #endif // EXECUTOR_HPP
static const unsigned int MINIMUM_NUMBER_THREADS
Definition: executor.hpp:81
error::KallistoDetailedResult shutdown()
Definition: bluetooth_adapter.hpp:52
static const unsigned int MAXIMUM_NUMBER_THREADS
Definition: executor.hpp:76
std::function< void()> ExecutorRunnable
Definition: executor.hpp:86
error::KallistoDetailedResult shutdownNow()
error::KallistoDetailedResult execute(ExecutorRunnable runnable)