18#include <Poco/Thread.h>
24#if defined(_WIN32) || !defined(_OPENMP)
25#include <Poco/Environment.h>
47 throw std::invalid_argument(
"NULL ThreadScheduler passed to ThreadPool constructor.");
49 if (numThreads == 0) {
69#if defined(_WIN32) || !defined(_OPENMP)
70 int physicalCores = Poco::Environment::processorCount();
77 if (!maxCores.is_initialized())
78 return std::min(maxCores.get_value_or(0), physicalCores);
96 throw std::runtime_error(
"Threads have already started.");
102 std::ostringstream name;
103 name <<
"Thread" << i;
105 auto thread = std::make_unique<Poco::Thread>(name.str());
107 auto runnable = std::make_unique<ThreadPoolRunnable>(i,
m_scheduler.get(),
m_prog.get(), waitSec);
108 thread->start(*runnable);
109 m_threads.emplace_back(std::move(thread));
165 runnable->clearWait();
const std::vector< double > & rhs
#define PARALLEL_GET_MAX_THREADS
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
A Task is a unit of work to be scheduled and run by a ThreadPool.
virtual double cost()
What is the computational cost of this task?
void schedule(const std::shared_ptr< Task > &task, bool start=false)
Schedule a task for later execution.
std::vector< std::unique_ptr< Poco::Thread > > m_threads
Vector with all the threads that are started.
ThreadPool(ThreadScheduler *scheduler=new ThreadSchedulerFIFO(), size_t numThreads=0, ProgressBase *prog=nullptr)
Constructor.
size_t m_numThreads
Number of cores used.
bool m_started
Have the threads started?
void start(double waitSec=0.0)
Start the threads and begin looking for tasks.
std::vector< std::unique_ptr< ThreadPoolRunnable > > m_runnables
Vector of the POCO-required classes to actually run in a thread.
std::unique_ptr< ProgressBase > m_prog
Progress reporter.
void joinAll()
Wait for all threads that have started to finish.
static size_t getNumPhysicalCores()
Return the number of physical cores available on the system.
std::unique_ptr< ThreadScheduler > m_scheduler
The ThreadScheduler instance taking care of task scheduling.
The ThreadScheduler object defines how tasks are allocated to threads and in what order.
bool compareTasks(Task *lhs, Task *rhs)
Method to perform sorting of task lists.