17class AbstractThreadPool {
21 virtual void destroy();
22 Thread(
const std::string &name, AbstractThreadPool *);
25 AbstractThreadPool *pool;
28 struct List :
public std::vector<AbstractThreadPool *> {
29 friend AbstractThreadPool;
33 static std::vector<AbstractThreadPool *> pools() {
return pools_; }
34 AbstractThreadPool(
const std::string &);
35 virtual ~AbstractThreadPool();
38 std::string name()
const {
return name_; }
42 virtual void appendThread(AbstractThreadPool::Thread *);
43 virtual void removeThread(AbstractThreadPool::Thread *);
44 std::vector<AbstractThreadPool::Thread *> threads_;
52 inline static List pools_;
60class ThreadPool :
public AbstractThreadPool {
71 static bool async(M m) {
72 return instance_.value->getThread()->invokeMethod(m);
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]() {
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); }); });
87 template <
typename M,
typename R>
88 static bool async(M m, R r) {
89 return async(instance_.value->getThread(), m, r);
92 static ThreadPool *instance() {
return instance_.value; }
94 ThreadPool(
const std::string &,
int = ThreadPool_DEFAULT_WORK_THREADS);
95 ThreadPool(
int workThreads = ThreadPool_DEFAULT_WORK_THREADS) : ThreadPool(
"ThreadPool", workThreads) {}
98 Thread *createThread(
const std::string &name = {});
100 void removeThread(AbstractThreadPool::Thread *)
override;
101 virtual void quit()
override;
103 AbstractThreadPool::Thread *getThread();
107 std::vector<AbstractThreadPool::Thread *> workThreads_;
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