25#ifndef JOIN_CORE_THREADPOOL_HPP
26#define JOIN_CORE_THREADPOOL_HPP
153 template <class Function, class... Args>
154 void push (Function&& func, Args&&... args)
157 _jobs.emplace_back (std::bind (std::forward<Function> (func), std::forward<Args> (args)...));
158 _condition.signal ();
164 size_t size () const noexcept;
177 std::atomic<
bool> _stop;
180 std::deque<
std::function<
void ()>> _jobs;
192 template <class InputIt, class Func>
196 int count = std::distance (first, last);
202 concurrency = std::min (concurrency, count);
203 int elements = count / concurrency;
204 int rest = count % concurrency;
206 std::vector<int> tasks (concurrency, elements);
207 for (
int i = 0; i < rest; ++i)
213 std::vector<Thread> pool;
214 int nth = concurrency - 1;
218 auto beg = first, end = first;
219 for (
int i = 0; i < nth; ++i)
221 std::advance (end, tasks[i]);
222 pool.emplace_back (function, beg, end);
227 function (beg, last);
230 for (
auto& thread : pool)
243 template <
class InputIt,
class Func>
246 distribute (first, last, [&function] (InputIt beg, InputIt end) {
247 for (; beg != end; ++beg)
condition variable class.
Definition condition.hpp:42
static const CpuTopology * instance()
get instance.
Definition cpu.cpp:50
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:37
class owning a mutex for the duration of a scoped block.
Definition mutex.hpp:246
thread pool class.
Definition threadpool.hpp:109
ThreadPool(ThreadPool &&other)=delete
move constructor.
ThreadPool(const ThreadPool &other)=delete
copy constructor.
thread class.
Definition thread.hpp:148
worker thread class.
Definition threadpool.hpp:49
WorkerThread & operator=(const WorkerThread &other)=delete
copy assignment.
friend class ThreadPool
friendship with ThreadPool.
Definition threadpool.hpp:102
WorkerThread(WorkerThread &&other)=delete
move constructor.
~WorkerThread() noexcept
destroy worker thread.
Definition threadpool.cpp:47
WorkerThread(const WorkerThread &other)=delete
copy constructor.
Definition acceptor.hpp:32
void distribute(InputIt first, InputIt last, Func function)
determine the number of threads and tasks per thread to run and execute them in parallel.
Definition threadpool.hpp:193
void parallelForEach(InputIt first, InputIt last, Func function)
parallel for each loop.
Definition threadpool.hpp:244