/*! \file pithreadpoolloop.h
* \ingroup Thread
* \~\brief
* \~english Thread pool loop
* \~russian Пул потоков
*/
/*
PIP - Platform Independent Primitives
Thread pool loop
Ivan Pelipenko peri4ko@yandex.ru
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 .
*/
#ifndef PITHREADPOOLLOOP_H
#define PITHREADPOOLLOOP_H
#include "pisemaphore.h"
#include "pivector.h"
class PIThread;
class PIP_EXPORT PIThreadPoolLoop {
public:
//! \~english
//! Contructs thread pool with threads count "thread_cnt".
//! If "thread_cnt" = -1 then system processors count used
//! \~russian
//! Создает пул из "thread_cnt" потоков. Если "thread_cnt" = -1
//! то используется количество процессоров системы
PIThreadPoolLoop(int thread_cnt = -1);
virtual ~PIThreadPoolLoop();
//! \~english Set threads function to [lambda expression](https://en.cppreference.com/w/cpp/language/lambda) "f" with format [ ](int){
//! ... }
//! \~russian Устанавливает функцию потоков на [лямбда-выражение](https://ru.cppreference.com/w/cpp/language/lambda) "f" в формате [
//! ](int){ ... }
void setFunction(std::function f);
//! \~english Wait for all threads stop
//! \~russian Ожидает завершения всех потоков
void wait();
//! \~english
//! Start functions execution with integer argument range
//! from "index_start" to "index_start + index_count - 1"
//! \~russian
//! Начинает исполнение потоков с аргументами по диапазону
//! от "index_start" до "index_start + index_count - 1"
void start(int index_start, int index_count);
//! \~english
//! Start functions execution with integer argument range
//! from "index_start" to "index_start + index_count - 1"
//! and wait for finish
//! \~russian
//! Начинает исполнение потоков с аргументами по диапазону
//! от "index_start" до "index_start + index_count - 1"
//! и ожидает завершения
void exec(int index_start, int index_count);
//! \~english
//! Start functions "f" execution with integer argument range
//! from "index_start" to "index_start + index_count - 1"
//! and wait for finish
//! \~russian
//! Начинает исполнение потоками функции "f" с аргументами по диапазону
//! от "index_start" до "index_start + index_count - 1"
//! и ожидает завершения
void exec(int index_start, int index_count, std::function f);
private:
PIVector threads;
std::function func;
PISemaphore sem_exec, sem_done;
std::atomic_bool is_destroy = {false};
std::atomic_int counter = {0}, wait_count = {0};
};
#endif