PIThreadPoolWorker ready to use

This commit is contained in:
2026-03-25 10:59:32 +03:00
parent 5868e0ec9d
commit a16f629dc5
4 changed files with 106 additions and 64 deletions

View File

@@ -1,7 +1,7 @@
/*
PIP - Platform Independent Primitives
Stephan Fomenko, Ivan Pelipenko
Ivan Pelipenko, Stephan Fomenko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -19,15 +19,25 @@
#include "pithreadpoolworker.h"
#include "piliterals_time.h"
#include "pisysteminfo.h"
//! \class PIThreadPoolWorker
//! \brief
//! Thread pools address two different problems: they usually provide improved performance when executing large
//! numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and
//! managing the resources, including threads, consumed when executing a collection of tasks.
//! \addtogroup Thread
//! \{
//! \class PIThreadPoolWorker pithreadpoolworker.h
//! \~\details
//! \~english
//! \PIThreadPoolWorker is a class that implements a fixed-size pool of worker threads for general-purpose task execution. It
//! allows starting threads, enqueuing tasks as functors or class member methods, and managing their execution. The class provides methods
//! to wait for all tasks or specific tasks by ID to complete, as well as to gracefully stop the pool with timeouts. The lifecycle of each
//! task can be monitored using the taskStarted and taskFinished events. It is a versatile tool for multithreaded asynchronous operations.
//! \~russian
//! PIThreadPoolWorker — это класс, реализующий фиксированный пул рабочих потоков для выполнения задач общего назначения. Он
//! позволяет запускать потоки, добавлять в очередь задачи в виде функторов или методов класса, а также управлять их выполнением. Класс
//! предоставляет методы для ожидания завершения всех задач или отдельных задач по их идентификатору, а также для корректной остановки пула
//! с таймаутами. С помощью событий taskStarted и taskFinished можно отслеживать жизненный цикл каждой задачи. Это универсальный инструмент
//! для многопоточного асинхронного выполнения операций.
//!
//! \}
PIThreadPoolWorker::PIThreadPoolWorker(int threads_count) {
@@ -205,7 +215,7 @@ PIThreadPoolWorker::TaskStatus PIThreadPoolWorker::taskStatus(int64_t id) const
if (w->in_work == id) return TaskStatus::InProgress;
for (const auto & t: *qref)
if (t.id == id) return TaskStatus::Enqueued;
return id <= next_task_id ? TaskStatus::Done : TaskStatus::Unknown;
return id <= next_task_id ? TaskStatus::DoneOrCancelled : TaskStatus::Unknown;
}