/*! \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 <http://www.gnu.org/licenses/>.
*/

#ifndef PITHREADPOOLLOOP_H
#define PITHREADPOOLLOOP_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<void(int)> 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<void(int)> f);

private:
	PIVector<PIThread * > threads;
	std::function<void(int)> func;
	std::atomic_int counter;

};


#endif
