34 static __thread
int depth = 0;
35 static __thread
bool is_leaf =
false;
39 threadpool::threadpool(
unsigned int max_threads) : running(
true), active(0) {
40 boost::thread::attributes attrs;
43 size_t i = max ? max - 1 : 0;
45 threads.push_back(boost::thread(attrs, boost::bind(&threadpool::run,
this,
false)));
49 threadpool::~threadpool() {
52 const boost::unique_lock<boost::mutex> lock(mutex);
54 has_work.notify_all();
61 has_work.notify_all();
63 for (
size_t i = 0; i<threads.size(); i++) {
64 try { threads[i].join(); }
69 void threadpool::submit(
waiter *obj, std::function<
void()> f,
bool leaf) {
71 boost::unique_lock<boost::mutex> lock(mutex);
72 if (!leaf && ((active == max && !queue.empty()) || depth > 0)) {
85 queue.push_front({obj, f, leaf});
87 queue.push_back({obj, f, leaf});
88 has_work.notify_one();
96 threadpool::waiter::~waiter()
100 boost::unique_lock<boost::mutex> lock(mt);
102 MERROR(
"wait should have been called before waiter dtor - waiting now");
109 catch (
const std::exception &e)
118 boost::unique_lock<boost::mutex> lock(mt);
123 void threadpool::waiter::inc() {
124 const boost::unique_lock<boost::mutex> lock(mt);
128 void threadpool::waiter::dec() {
129 const boost::unique_lock<boost::mutex> lock(mt);
135 void threadpool::run(
bool flush) {
136 boost::unique_lock<boost::mutex> lock(mutex);
139 while(queue.empty() && running)
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
#define THREAD_STACK_SIZE