58        threadpool(
size_t threads = std::thread::hardware_concurrency())
 
   62                throw std::runtime_error(
"failed to construct threadpool with zero threads");
 
   67            _workers.reserve(threads);
 
   69            while (_workers.size() < _workers.capacity())
 
   71                _workers.push_back(std::thread([&]()
 
   73                std::function<void()> task;
 
   74                while (pop_task(task))
 
 
  100        template <
class F, 
class... Args>
 
  101        std::future<std::result_of_t<F(Args...)>> 
submit(F &&function, Args &&...args)
 
  103            std::shared_ptr<std::packaged_task<std::result_of_t<F(Args...)>()>> packaged_task;
 
  104            std::function<void()> work;
 
  105            std::future<std::result_of_t<F(Args...)>> result;
 
  107            packaged_task = std::make_shared<std::packaged_task<std::result_of_t<F(Args...)>()>>(std::bind(std::forward<F>(function), std::forward<Args>(args)...));
 
  108            result = packaged_task->get_future();
 
  110            work = [packaged_task]()
 
  115            push_task(std::move(work));
 
 
  137                throw std::runtime_error(
"failed to shut down inactive threadpool");
 
  140            _condition.notify_all();
 
  141            for (std::thread &worker : _workers)
 
 
  148        void push_task(std::function<
void()> &&task)
 
  150            std::unique_lock<std::mutex> lock(_mutex);
 
  153                throw std::runtime_error(
"failed to submit to inactive threadpool");
 
  155            _tasks.push(std::move(task));
 
  157            _condition.notify_one();
 
  160        bool pop_task(std::function<
void()> &task)
 
  162            std::unique_lock<std::mutex> lock(_mutex);
 
  168                    return (!_active || !_tasks.empty());
 
  172                task = _tasks.front();
 
  182        std::vector<std::thread> _workers;
 
  183        std::queue<std::function<void()>> _tasks;
 
  185        std::condition_variable _condition;
 
 
 
Definition threadpool.hpp:49
~threadpool()
destroys threadpool after calling shutdown if necessary
Definition threadpool.hpp:83
threadpool(size_t threads=std::thread::hardware_concurrency())
constructs a new threadpool
Definition threadpool.hpp:58
bool active() const
check if the threadpool is active
Definition threadpool.hpp:124
void shutdown()
shut down threadpool by joining threads and rejecting submissions
Definition threadpool.hpp:133
std::future< std::result_of_t< F(Args...)> > submit(F &&function, Args &&...args)
submits a function with its arguments to the threadpool
Definition threadpool.hpp:101
Definition threadpool.hpp:42