-#include "thr_pool.h"
-
-// if blocking, then addJob() blocks when queue is full
-// otherwise, addJob() simply returns false when queue is full
-thread_pool::thread_pool(size_t sz, bool blocking)
-: nthreads_(sz),blockadd_(blocking),jobq_(100*sz) {
- for (size_t i=0; i<nthreads_; i++)
- th_.emplace_back(&thread_pool::do_worker, this);
-}
-
-// IMPORTANT: this function can be called only when no external thread
-// will ever use this thread pool again or is currently blocking on it
-thread_pool::~thread_pool() {
- for (size_t i=0; i<nthreads_; i++)
- jobq_.enq(job_t());
-
- for (size_t i=0; i<nthreads_; i++)
- th_[i].join();
-}
-
-bool thread_pool::addJob(const job_t & j) {
- return jobq_.enq(j,blockadd_);
-}
-
-void thread_pool::do_worker() {
- job_t j;
- while (1) {
- jobq_.deq(&j);
- if (!j)
- break;
- j();
- }
-}