25#ifndef JOIN_CORE_THREAD_POOL_HPP
26#define JOIN_CORE_THREAD_POOL_HPP
152 template <class
Function, class... Args>
156 _jobs.emplace_back (std::bind (std::forward<Function> (func), std::forward<Args> (args)...));
157 _condition.signal ();
163 size_t size () const noexcept;
179 std::deque<
std::function<
void ()>> _jobs;
191 template <class InputIt, class Func>
195 int count = std::distance (first, last);
201 concurrency = std::min (concurrency, count);
202 int elements = count / concurrency;
203 int rest = count % concurrency;
205 std::vector<int> tasks (concurrency, elements);
206 for (
int i = 0; i < rest; ++i)
212 std::vector<Thread> pool;
213 int nth = concurrency - 1;
217 auto beg = first, end = first;
218 for (
int i = 0; i < nth; ++i)
220 std::advance (end, tasks[i]);
221 pool.emplace_back (function, beg, end);
226 function (beg, last);
229 for (
auto& thread : pool)
242 template <
class InputIt,
class Func>
245 distribute (first, last, [&function] (InputIt beg, InputIt end) {
246 for (; beg != end; ++beg)
condition variable class.
Definition condition.hpp:42
static const CpuTopology * instance()
get instance.
Definition cpu.cpp:49
fixed-capacity move-only allocation-free alternative to std::function.
Definition function.hpp:44
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:249
thread pool class.
Definition thread_pool.hpp:108
ThreadPool(ThreadPool &&other)=delete
move constructor.
ThreadPool(const ThreadPool &other)=delete
copy constructor.
thread class.
Definition thread.hpp:180
worker thread class.
Definition thread_pool.hpp:48
WorkerThread & operator=(const WorkerThread &other)=delete
copy assignment.
friend class ThreadPool
friendship with ThreadPool.
Definition thread_pool.hpp:101
WorkerThread(WorkerThread &&other)=delete
move constructor.
~WorkerThread() noexcept
destroy worker thread.
Definition thread_pool.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 thread_pool.hpp:192
void parallelForEach(InputIt first, InputIt last, Func function)
parallel for each loop.
Definition thread_pool.hpp:243