Fixed two major bugs in paxos.cc.
[invirt/third/libt4.git] / rpc / thr_pool.cc
1 #include "thr_pool.h"
2
3 // if blocking, then addJob() blocks when queue is full
4 // otherwise, addJob() simply returns false when queue is full
5 ThrPool::ThrPool(size_t sz, bool blocking)
6 : nthreads_(sz),blockadd_(blocking),jobq_(100*sz) {
7     for (size_t i=0; i<nthreads_; i++)
8         th_.emplace_back(&ThrPool::do_worker, this);
9 }
10
11 // IMPORTANT: this function can be called only when no external thread 
12 // will ever use this thread pool again or is currently blocking on it
13 ThrPool::~ThrPool() {
14     for (size_t i=0; i<nthreads_; i++)
15         jobq_.enq(job_t());
16
17     for (size_t i=0; i<nthreads_; i++)
18         th_[i].join();
19 }
20
21 bool ThrPool::addJob(const job_t &j) {
22     return jobq_.enq(j,blockadd_);
23 }
24
25 void ThrPool::do_worker() {
26     job_t j;
27     while (1) {
28         jobq_.deq(&j);
29         if (!j)
30             break;
31         j();
32     }
33 }