Libftpp
A modern C++ library
persistent_worker.hpp
Go to the documentation of this file.
1 #ifndef PERSISTENT_WORKER_HPP
2 #define PERSISTENT_WORKER_HPP
3 
4 #include <condition_variable>
5 #include <functional>
6 #include <mutex>
7 #include <stack>
8 #include <string>
9 #include <thread>
10 #include <unordered_map>
11 
12 #define PAUSE_BT_TASK 0 // ms
13 
58 {
59 private:
60  std::mutex _mtx;
61  std::condition_variable _cv;
62  std::unordered_map<std::string, std::function<void()>> _tasks;
63  bool _running = true;
64  std::thread _thread;
65 
66  void loop();
67 
68 public:
71 
72  void addTask(const std::string& name, const std::function<void()>& jobToExecute);
73 
74  void removeTask(const std::string& name);
75 };
76 #endif
Persistent worker thread that executes tasks in a continuous loop.
void removeTask(const std::string &name)
void addTask(const std::string &name, const std::function< void()> &jobToExecute)