AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
ThreadPool.h
1/*
2Copyright (c) 2026 Alexandr Kuzmuk
3
4This file is part of the AsyncFw project. Licensed under the MIT License.
5See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
6*/
7
8#pragma once
9
10#include "../core/Thread.h"
11#include "Instance.h"
12
13#define ThreadPool_DEFAULT_WORK_THREADS 1
14
15namespace AsyncFw {
17class AbstractThreadPool {
18public:
19 class Thread : public AsyncFw::Thread {
20 public:
21 virtual void destroy();
22 Thread(const std::string &name, AbstractThreadPool *);
23
24 protected:
25 AbstractThreadPool *pool;
26 };
27
28 struct List : public std::vector<AbstractThreadPool *> {
29 friend AbstractThreadPool;
30 ~List();
31 };
32
33 static std::vector<AbstractThreadPool *> pools() { return pools_; }
34 AbstractThreadPool(const std::string &);
35 virtual ~AbstractThreadPool();
36 virtual void quit();
37 AbstractThread *thread() { return thread_; }
38 std::string name() const { return name_; }
39 AbstractThread::LockGuard threads(std::vector<AbstractThreadPool::Thread *> **);
40
41protected:
42 virtual void appendThread(AbstractThreadPool::Thread *);
43 virtual void removeThread(AbstractThreadPool::Thread *);
44 std::vector<AbstractThreadPool::Thread *> threads_;
45 std::mutex mutex;
46 AbstractThread *thread_;
47
48private:
49 struct Compare {
50 bool operator()(const AbstractThread *t1, const AbstractThread *t2) const { return t1 < t2; }
51 };
52 inline static List pools_;
53 std::string name_;
54};
55
60class ThreadPool : public AbstractThreadPool {
61public:
62 template <typename M>
63 static bool sync(AbstractThread *_t, M m) {
64 return _t->invokeMethod(m, true);
65 }
66 template <typename M>
67 static bool async(AbstractThread *_t, M m) {
68 return _t->invokeMethod(m);
69 }
70 template <typename M>
71 static bool async(M m) {
72 return instance_.value->getThread()->invokeMethod(m);
73 }
74 template <typename M, typename R, typename T = std::invoke_result<M>::type>
75 static typename std::enable_if<std::is_void<T>::value, bool>::type async(AbstractThread *thread, M method, R result) {
77 return thread->invokeMethod([_t, method, result]() {
78 method();
79 _t->invokeMethod([result]() { result(); });
80 });
81 }
82 template <typename M, typename R, typename T = std::invoke_result<M>::type>
83 static typename std::enable_if<!std::is_void<T>::value, bool>::type async(AbstractThread *thread, M method, R result) {
85 return thread->invokeMethod([_t, method, result]() { _t->invokeMethod([v = std::move(method()), result]() { result(v); }); });
86 }
87 template <typename M, typename R>
88 static bool async(M m, R r) {
89 return async(instance_.value->getThread(), m, r);
90 }
91
92 static ThreadPool *instance() { return instance_.value; }
93
94 ThreadPool(const std::string &, int = ThreadPool_DEFAULT_WORK_THREADS);
95 ThreadPool(int workThreads = ThreadPool_DEFAULT_WORK_THREADS) : ThreadPool("ThreadPool", workThreads) {}
96 ~ThreadPool();
97
98 Thread *createThread(const std::string &name = {});
99
100 void removeThread(AbstractThreadPool::Thread *) override;
101 virtual void quit() override;
102
103 AbstractThreadPool::Thread *getThread();
104
105private:
106 inline static AsyncFw::Instance<ThreadPool> instance_ {"ThreadPool"};
107 std::vector<AbstractThreadPool::Thread *> workThreads_;
108 int workThreadsSize;
109};
110} // namespace AsyncFw
The AbstractThread class provides the base functionality for thread management.
Definition AbstractThread.h:46
std::lock_guard< std::mutex > LockGuard
The LockGuard type.
Definition AbstractThread.h:54
std::enable_if< std::is_void< typenamestd::invoke_result< M >::type >::value, bool >::type invokeMethod(M method, bool sync=false) const
Runs a method in a managed thread.
Definition AbstractThread.h:74
static AbstractThread * currentThread()
Returns a pointer to the AsyncFw::AbstractThread that manages the currently executing thread.
Definition AbstractThread.cpp:295
The Instance class.
Definition Instance.h:40
AsyncFw::Thread thread with sockets.
Definition Thread.h:16