18#include <AUI/Common/AVector.h>
19#include <AUI/Common/AQueue.h>
20#include <AUI/Common/AException.h>
21#include <AUI/Thread/AThread.h>
24#include "AUI/Traits/concepts.h"
35 class API_AUI_CORE Worker :
public AThread {
38 bool processQueue(std::unique_lock<std::mutex>& mutex,
AQueue<std::function<
void()>>& queue);
41 void iteration(std::unique_lock<std::mutex>& tpLock);
42 void wait(std::unique_lock<std::mutex>& tpLock);
49 template <aui::predicate ShouldContinue>
50 void loop(ShouldContinue&& shouldContinue) {
51 std::unique_lock lock(mTP.mQueueLock);
52 while (shouldContinue()) {
54 if (!shouldContinue()) {
71 typedef std::function<void()> task;
77 std::mutex mQueueLock;
78 std::condition_variable mCV;
79 size_t mIdleWorkers = 0;
94 size_t getPendingTaskCount();
95 size_t getTotalTaskCount() {
96 return getPendingTaskCount() + getTotalWorkerCount() - getIdleWorkerCount();
98 void run(
const std::function<
void()>& fun, Priority priority = PRIORITY_MEDIUM);
100 void runLaterTasks();
101 static void enqueue(
const std::function<
void()>& fun, Priority priority = PRIORITY_MEDIUM);
103 void setWorkersCount(std::size_t workersCount);
106 std::unique_lock lck(mQueueLock);
120 size_t getTotalWorkerCount()
const {
return mWorkers.size(); }
121 size_t getIdleWorkerCount()
const {
return mIdleWorkers; }
142 template <
typename Iterator,
typename Functor>
143 auto parallel(Iterator begin, Iterator end, Functor&& functor);
145 template <aui::invocable Callable>
146 [[nodiscard]]
inline auto operator*(Callable fun) {
147 using Value = std::invoke_result_t<Callable>;
148 AFuture<Value> future(std::move(fun));
150 [innerWeak = future.inner().weak()]() {
155 if (auto lock = innerWeak.lock()) {
156 auto innerUnsafePointer = lock->ptr().get();
161 innerUnsafePointer->tryExecute(innerWeak);
165 AThreadPool::PRIORITY_LOWEST);
172#include <AUI/Thread/AFuture.h>
184template <
typename T =
void>
187 using super = AVector<AFuture<T>>;
225 template <aui::invocable OnComplete>
238 OnComplete onComplete;
240 std::atomic_bool canBeCalled =
true;
242 auto temporary = _new<Temporary>(std::forward<OnComplete>(onComplete), *
this);
245 for (
const AFuture<T>& v : temporary->myCopy) {
251 if (temporary->canBeCalled.exchange(
false)) {
252 temporary->onComplete();
259template <
typename Iterator,
typename Functor>
261 using ResultType =
decltype(std::declval<Functor>()(std::declval<Iterator>(), std::declval<Iterator>()));
264 size_t itemCount = end - begin;
268 size_t itemsPerThread = itemCount / affinity;
270 for (
size_t threadIndex = 0; threadIndex < affinity; ++threadIndex) {
271 auto forThreadBegin = begin;
272 begin += itemsPerThread;
273 auto forThreadEnd = threadIndex + 1 == affinity ? end : begin;
275 *
this * [functor = std::forward<Functor>(functor), forThreadBegin, forThreadEnd]() ->
decltype(
auto) {
276 return functor(forThreadBegin, forThreadEnd);
283#include <AUI/Reflect/AReflect.h>
Manages multiple futures.
Definition AThreadPool.h:185
void waitForAll()
Wait for the result of every AFuture.
Definition AThreadPool.h:196
void checkForExceptions() const
Find AFutures that encountered an exception. If such AFuture is found, AInvocationTargetException is ...
Definition AThreadPool.h:207
void onAllComplete(OnComplete &&onComplete)
Specifies a callback which will be called when all futures in future set would have the result.
Definition AThreadPool.h:226
Represents a value that will be available at some point in the future.
Definition AFuture.h:621
const AFuture & onSuccess(Callback &&callback) const noexcept
Add onSuccess callback to the future.
Definition AFuture.h:709
A std::queue with AUI extensions.
Definition AQueue.h:24
Definition AThreadPool.h:169
static AThreadPool & global()
Global thread pool created with the default constructor.
AThreadPool(size_t size)
Initializes the thread pool with size of threads.
auto parallel(Iterator begin, Iterator end, Functor &&functor)
Definition AThreadPool.h:260
AThreadPool()
Initializes the thread pool with max(std::thread::hardware_concurrency() - 1, 2) of threads or –aui-t...
A std::vector with AUI extensions.
Definition AVector.h:39
bool hasResult() const noexcept
Definition AFuture.h:395
Definition iterators.h:34