13 #include <condition_variable>
22 std::condition_variable waitVar;
26 std::atomic<int> tasksLeft{ 1 };
32 std::exception_ptr caughtException =
nullptr;
39 virtual void wait()
override;
66 void removeReference();
81 std::queue<SharedPtr<Task>> tasks;
84 std::condition_variable taskVar;
88 std::condition_variable waitVar;
92 std::atomic<bool> stop;
95 std::atomic<int> tasksLeft;
138 granularity = newGranularity;
149 void processTask(
const bool wait);
Generic dynamically allocated resizable storage.
Generic wrappers of lambdas, functors and other callables.
uint32_t Size
Integral type used to index arrays (by default).
#define NAMESPACE_SPH_END
Wrapper of type value of which may or may not be present.
Interface for executing tasks (potentially) asynchronously.
Generic dynamically allocated resizable storage.
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Handle used to control tasks submitted into the scheduler.
Task to be executed by one of available threads.
virtual bool completed() const override
Checks if the task already finished.
static SharedPtr< Task > getCurrent()
Returns the currently execute task, or nullptr if no task is currently executed on this thread.
SharedPtr< Task > getParent() const
Task(const Function< void()> &callable)
virtual void wait() override
Waits till the task and all the child tasks are completed.
bool isRoot() const
Returns true if this is the top-most task.
void setException(std::exception_ptr exception)
Saves exception into the task.
void setParent(SharedPtr< Task > parent)
Assigns a task that spawned this task.
Thread pool capable of executing tasks concurrently.
ThreadPool(const Size numThreads=0, const Size granularity=1000)
Initialize thread pool given the number of threads to use.
Size remainingTaskCnt()
Returns the number of unfinished tasks.
virtual SharedPtr< ITask > submit(const Function< void()> &task) override
Submits a task into the thread pool.
static SharedPtr< ThreadPool > getGlobalInstance()
Returns the global instance of the thread pool.
void waitForAll()
Blocks until all submitted tasks has been finished.
virtual Size getRecommendedGranularity() const override
Returns a value of granularity that is expected to perform well with the current thread count.
virtual Optional< Size > getThreadIdx() const override
Returns the index of this thread, or NOTHING if this thread was not invoked by the thread pool.
virtual Size getThreadCnt() const override
Returns the number of threads used by this thread pool.
void setGranularity(const Size newGranularity)
Modifies the default granularity of the thread pool.