Libftpp
A modern C++ library
worker_pool.hpp
Go to the documentation of this file.
1 #ifndef WORKER_POOL_HPP
2 #define WORKER_POOL_HPP
3 
4 #include <condition_variable>
5 #include <functional>
6 #include <mutex>
7 #include <queue>
8 #include <thread>
9 #include <vector>
44 {
45 
46 private:
47  size_t _size = 0;
48  std::vector<std::thread> _workers;
49  std::queue<std::function<void()>> _jobs;
50  std::mutex _mtx;
51  std::condition_variable _cv;
52  bool _stop = false;
53 
54 public:
62  class IJobs
63  {
64  private:
65  public:
66  virtual ~IJobs() {};
67  virtual void start() = 0;
68  };
69 
70  WorkerPool(const int& nbWorkers);
71  ~WorkerPool();
72 
73  void loop();
74  void addJob(const std::function<void()>& jobToExecute);
75  void addJob(IJobs& jobToExecute);
76 };
77 #endif
Interface for defining jobs to be executed by the worker pool. Classes implementing this interface mu...
Definition: worker_pool.hpp:63
virtual void start()=0
Worker Pool for Concurrent Job Execution.
Definition: worker_pool.hpp:44
void loop()
Definition: worker_pool.cpp:13
void addJob(const std::function< void()> &jobToExecute)
Definition: worker_pool.cpp:54
WorkerPool(const int &nbWorkers)
Definition: worker_pool.cpp:3