Files
pip/libs/main/thread/pithreadpoolexecutor.h
2026-03-20 16:31:30 +03:00

96 lines
4.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! \~\file pithreadpoolexecutor.h
//! \~\ingroup Thread
//! \brief
//! \~english Thread pool executor
//! \~russian Исполнитель пула потоков
//!
//! \details
//! \~english Executes tasks in a pool of worker threads.
//! \~russian Выполняет задачи в пуле рабочих потоков.
/*
PIP - Platform Independent Primitives
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PITHREADPOOLEXECUTOR_H
#define PITHREADPOOLEXECUTOR_H
#include "piblockingqueue.h"
#include "pithread.h"
#include <atomic>
//! \~\ingroup Thread
//! \~\brief
//! \~english Fixed-size pool of worker threads for fire-and-forget tasks.
//! \~russian Фиксированный пул рабочих потоков для задач без ожидания результата.
class PIP_EXPORT PIThreadPoolExecutor {
public:
//! \~english Constructs executor with \a corePoolSize worker threads.
//! \~russian Создает исполнитель с \a corePoolSize рабочими потоками.
explicit PIThreadPoolExecutor(int corePoolSize);
//! \~english Stops worker threads and destroys executor resources.
//! \~russian Останавливает рабочие потоки и уничтожает ресурсы исполнителя.
virtual ~PIThreadPoolExecutor();
//! \~\brief
//! \~english Submits \a runnable for asynchronous execution by a worker thread.
//! \~russian Передает \a runnable на асинхронное выполнение рабочим потоком.
//! \details
//! \~english
//! This is a best-effort fire-and-forget call and does not report whether the task was accepted.
//! \After shutdown requests new tasks are ignored.
//! \~russian
//! Это вызов по принципу best-effort без ожидания результата и без сообщения о том, была ли задача принята.
//! \После запроса на завершение новые задачи игнорируются.
void execute(std::function<void()> runnable);
//! \~english Requests immediate shutdown and stops worker threads without waiting for queued tasks to finish.
//! \~russian Запрашивает немедленное завершение и останавливает рабочие потоки без ожидания завершения задач в очереди.
void shutdownNow();
//! \~\brief
//! \~english Requests orderly shutdown: new tasks are rejected and workers stop after the current queue is drained.
//! \~russian Запрашивает упорядоченное завершение: новые задачи отклоняются, а рабочие потоки останавливаются после опустошения текущей
//! очереди.
//! \details
//! \~english This method does not wait for worker termination.
//! \~russian Этот метод не ожидает завершения рабочих потоков.
void shutdown();
//! \~english Returns \c true after \a shutdown() or \a shutdownNow() has been requested.
//! \~russian Возвращает \c true после запроса \a shutdown() или \a shutdownNow().
bool isShutdown() const;
//! \~\brief
//! \~english Waits up to \a timeout for all worker threads to finish.
//! \~russian Ожидает до \a timeout завершения всех рабочих потоков.
//! \return
//! \~english \c false if the timeout expires first.
//! \~russian \c false, если таймаут истек раньше.
bool awaitTermination(PISystemTime timeout);
private:
std::atomic_bool isShutdown_;
PIBlockingQueue<std::function<void()>> taskQueue;
PIVector<PIThread *> threadPool;
};
#endif // PITHREADPOOLEXECUTOR_H