Clean-ups
[invirt/third/libt4.git] / rpc / thread_pool.cc
1 #include "thread_pool.h"
2
3 thread_pool::thread_pool(size_t sz) : th_(sz) {
4     for (auto & t : th_)
5         t = thread(&thread_pool::do_worker, this);
6 }
7
8 // this function can be called only when no external thread will
9 // ever use this thread pool again or is currently blocking on it
10 thread_pool::~thread_pool() {
11     for (size_t i=0; i<th_.size(); i++)
12         jobq_.enq(job_t());
13
14     for (auto & t : th_)
15         t.join();
16 }
17
18 bool thread_pool::addJob(const job_t & j) {
19     return jobq_.enq(j, false);
20 }
21
22 void thread_pool::do_worker() {
23     while (auto j = jobq_.deq())
24         j();
25 }