thread_pool: add optional ID to thread pool

this allows to prepend an ID to each thread pool to
better differentiate workers from different pools
master
Andre Puschmann 3 years ago committed by Xavier Arteaga
parent 4d9d882d75
commit 3b5344b0f7

@ -54,8 +54,8 @@ public:
virtual void work_imp() = 0;
private:
uint32_t my_id = 0;
thread_pool* my_parent = nullptr;
uint32_t my_id = 0;
thread_pool* my_parent = nullptr;
std::atomic<bool> running = {true};
void run_thread();
@ -64,22 +64,24 @@ public:
bool is_stopped() const;
};
thread_pool(uint32_t nof_workers);
void init_worker(uint32_t id, worker*, uint32_t prio = 0, uint32_t mask = 255);
void stop();
worker* wait_worker_id(uint32_t id);
worker* wait_worker(uint32_t tti);
worker* wait_worker_nb(uint32_t tti);
void start_worker(worker*);
void start_worker(uint32_t id);
worker* get_worker(uint32_t id);
uint32_t get_nof_workers();
thread_pool(uint32_t nof_workers_, std::string id_ = "");
void init_worker(uint32_t id, worker*, uint32_t prio = 0, uint32_t mask = 255);
void stop();
worker* wait_worker_id(uint32_t id);
worker* wait_worker(uint32_t tti);
worker* wait_worker_nb(uint32_t tti);
void start_worker(worker*);
void start_worker(uint32_t id);
worker* get_worker(uint32_t id);
uint32_t get_nof_workers();
std::string get_id();
private:
bool find_finished_worker(uint32_t tti, uint32_t* id);
typedef enum { STOP, IDLE, START_WORK, WORKER_READY, WORKING } worker_status;
std::string id; // id is prepended to every worker
std::vector<worker*> workers = {};
uint32_t nof_workers = 0;
uint32_t max_workers = 0;

@ -41,7 +41,7 @@ void thread_pool::worker::setup(uint32_t id, thread_pool* parent, uint32_t prio,
void thread_pool::worker::run_thread()
{
set_name(std::string("WORKER") + std::to_string(my_id));
set_name(my_parent->get_id() + std::string("WORKER") + std::to_string(my_id));
while (running.load(std::memory_order_relaxed)) {
wait_to_start();
if (running.load(std::memory_order_relaxed)) {
@ -61,9 +61,9 @@ uint32_t thread_pool::worker::get_id()
return my_id;
}
thread_pool::thread_pool(uint32_t max_workers_) : workers(max_workers_), status(max_workers_), cvar_worker(max_workers_)
thread_pool::thread_pool(uint32_t max_workers_, std::string id_) :
workers(max_workers_), max_workers(max_workers_), status(max_workers_), cvar_worker(max_workers_), id(id_)
{
max_workers = max_workers_;
for (uint32_t i = 0; i < max_workers; i++) {
workers[i] = NULL;
status[i] = IDLE;
@ -253,6 +253,11 @@ uint32_t thread_pool::get_nof_workers()
return nof_workers;
}
std::string thread_pool::get_id()
{
return id;
}
/**************************************************************************
* task_thread_pool - uses a queue to enqueue callables, that start
* once a worker is available

Loading…
Cancel
Save