threadpool: allow constructing an object, and misc tweaks
use unsigned int to avoid having to range check negative numbers, use const where possible, don't needlessly create empty objects, use std::move where possible
This commit is contained in:
parent
6b9d9f56a1
commit
db55263b4c
|
@ -39,11 +39,11 @@ static __thread int depth = 0;
|
||||||
|
|
||||||
namespace tools
|
namespace tools
|
||||||
{
|
{
|
||||||
threadpool::threadpool() : running(true), active(0) {
|
threadpool::threadpool(unsigned int max_threads) : running(true), active(0) {
|
||||||
boost::thread::attributes attrs;
|
boost::thread::attributes attrs;
|
||||||
attrs.set_stack_size(THREAD_STACK_SIZE);
|
attrs.set_stack_size(THREAD_STACK_SIZE);
|
||||||
max = tools::get_max_concurrency();
|
max = max_threads ? max_threads : tools::get_max_concurrency();
|
||||||
size_t i = max;
|
unsigned int i = max;
|
||||||
while(i--) {
|
while(i--) {
|
||||||
threads.push_back(boost::thread(attrs, boost::bind(&threadpool::run, this)));
|
threads.push_back(boost::thread(attrs, boost::bind(&threadpool::run, this)));
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ void threadpool::submit(waiter *obj, std::function<void()> f) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int threadpool::get_max_concurrency() {
|
unsigned int threadpool::get_max_concurrency() const {
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,9 @@ public:
|
||||||
static threadpool instance;
|
static threadpool instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
static threadpool *getNewForUnitTests(unsigned max_threads = 0) {
|
||||||
|
return new threadpool(max_threads);
|
||||||
|
}
|
||||||
|
|
||||||
// The waiter lets the caller know when all of its
|
// The waiter lets the caller know when all of its
|
||||||
// tasks are completed.
|
// tasks are completed.
|
||||||
|
@ -66,11 +69,12 @@ public:
|
||||||
// task to finish.
|
// task to finish.
|
||||||
void submit(waiter *waiter, std::function<void()> f);
|
void submit(waiter *waiter, std::function<void()> f);
|
||||||
|
|
||||||
int get_max_concurrency();
|
unsigned int get_max_concurrency() const;
|
||||||
|
|
||||||
|
~threadpool();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
threadpool();
|
threadpool(unsigned int max_threads = 0);
|
||||||
~threadpool();
|
|
||||||
typedef struct entry {
|
typedef struct entry {
|
||||||
waiter *wo;
|
waiter *wo;
|
||||||
std::function<void()> f;
|
std::function<void()> f;
|
||||||
|
@ -79,8 +83,8 @@ public:
|
||||||
boost::condition_variable has_work;
|
boost::condition_variable has_work;
|
||||||
boost::mutex mutex;
|
boost::mutex mutex;
|
||||||
std::vector<boost::thread> threads;
|
std::vector<boost::thread> threads;
|
||||||
int active;
|
unsigned int active;
|
||||||
int max;
|
unsigned int max;
|
||||||
bool running;
|
bool running;
|
||||||
void run();
|
void run();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue