move requests to global threadpool
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
#include <llmr/util/threadpool.hpp>
|
||||
#include <thread>
|
||||
|
||||
using namespace llmr::util;
|
||||
|
||||
std::unique_ptr<Threadpool> llmr::util::threadpool = std::make_unique<Threadpool>(std::thread::hardware_concurrency());
|
||||
|
||||
Threadpool::Threadpool(int max_workers)
|
||||
: max_workers(max_workers) {
|
||||
}
|
||||
|
||||
void Threadpool::add(Callback callback, void *data) {
|
||||
if (worker_count < max_workers) {
|
||||
workers.emplace_front(*this);
|
||||
worker_count++;
|
||||
workers.emplace_front(*this, std::string("worker ") + std::to_string(worker_count));
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
@@ -18,8 +21,9 @@ void Threadpool::add(Callback callback, void *data) {
|
||||
pthread_cond_signal(&condition);
|
||||
}
|
||||
|
||||
Threadpool::Worker::Worker(Threadpool& pool)
|
||||
: pool(pool) {
|
||||
Threadpool::Worker::Worker(Threadpool& pool, const std::string name)
|
||||
: pool(pool),
|
||||
name(name) {
|
||||
pthread_create(&thread, nullptr, loop, (void *)this);
|
||||
}
|
||||
|
||||
@@ -32,7 +36,7 @@ void *Threadpool::Worker::loop(void *ptr) {
|
||||
Worker *worker = static_cast<Worker *>(ptr);
|
||||
Threadpool& pool = worker->pool;
|
||||
|
||||
pthread_setname_np("worker");
|
||||
pthread_setname_np(worker->name.c_str());
|
||||
pthread_mutex_lock(&pool.mutex);
|
||||
while (true) {
|
||||
if (pool.tasks.size()) {
|
||||
|
||||
Reference in New Issue
Block a user