AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
AsyncFw::ThreadPool Class Reference

Управляет набором многократно используемых рабочих потоков для параллельного выполнения задач, вместо создания нового потока для каждой задачи. More...

#include <AsyncFw/ThreadPool>

Inheritance diagram for AsyncFw::ThreadPool:
[legend]
Collaboration diagram for AsyncFw::ThreadPool:
[legend]

Public Member Functions

 ThreadPool (const std::string &, int=ThreadPool_DEFAULT_WORK_THREADS)
 
 ThreadPool (int workThreads=ThreadPool_DEFAULT_WORK_THREADS)
 
Thread * createThread (const std::string &name={})
 
void removeThread (AbstractThreadPool::Thread *) override
 
virtual void quit () override
 
AbstractThreadPool::Thread * getThread ()
 
- Public Member Functions inherited from AsyncFw::AbstractThreadPool
 AbstractThreadPool (const std::string &)
 
AbstractThreadthread ()
 
std::string name () const
 
AbstractThread::LockGuard threads (std::vector< AbstractThreadPool::Thread * > **)
 

Static Public Member Functions

template<typename M>
static bool sync (AbstractThread *_t, M m)
 
template<typename M>
static bool async (AbstractThread *_t, M m)
 
template<typename M>
static bool async (M m)
 
template<typename M, typename R, typename T = std::invoke_result<M>::type>
static std::enable_if< std::is_void< T >::value, bool >::type async (AbstractThread *thread, M method, R result)
 
template<typename M, typename R, typename T = std::invoke_result<M>::type>
static std::enable_if<!std::is_void< T >::value, bool >::type async (AbstractThread *thread, M method, R result)
 
template<typename M, typename R>
static bool async (M m, R r)
 
static ThreadPoolinstance ()
 
- Static Public Member Functions inherited from AsyncFw::AbstractThreadPool
static std::vector< AbstractThreadPool * > pools ()
 

Additional Inherited Members

- Protected Member Functions inherited from AsyncFw::AbstractThreadPool
virtual void appendThread (AbstractThreadPool::Thread *)
 
- Protected Attributes inherited from AsyncFw::AbstractThreadPool
std::vector< AbstractThreadPool::Thread * > threads_
 
std::mutex mutex
 
AbstractThreadthread_
 

Detailed Description

Управляет набором многократно используемых рабочих потоков для параллельного выполнения задач, вместо создания нового потока для каждой задачи.

Благодаря постановке задач в очередь и повторному использованию потоков, повышается производительность системы, ограничивается использование ресурсов и снижаются накладные расходы на создание/уничтожение потоков.

#include <AsyncFw/MainThread>
#include <AsyncFw/ThreadPool>
#include <AsyncFw/Log>
class Pool : public AsyncFw::ThreadPool { // example for AsyncFw::Instance<AsyncFw::ThreadPool>::create<Pool>
public:
using AsyncFw::ThreadPool::ThreadPool;
std::string text() { return "Pool"; }
};
int main(int argc, char *argv[]) {
AsyncFw::LogStream::setTimeFormat("%Y-%m-%d %H:%M:%S", true);
Pool *threadPool = AsyncFw::Instance<AsyncFw::ThreadPool>::create<Pool>("ExampeThreadPool");
logInfo() << threadPool->text();
AsyncFw::AbstractThread *_lt = AsyncFw::ThreadPool::instance()->createThread("LogThread");
_lt->invokeMethod([]() { AsyncFw::Instance<AsyncFw::Log>::create(); }, true); //create log instance in log thread _lt
AsyncFw::AbstractThread *_t = AsyncFw::ThreadPool::instance()->createThread("SyncExample");
AsyncFw::ThreadPool::sync(_t, []() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
logInfo() << "sync run in thread" << ct->name() << ct->id();
});
AsyncFw::ThreadPool::async([]() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
logInfo() << "async run in thread" << ct->name() << ct->id();
});
AsyncFw::ThreadPool::async(
[]() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
logInfo() << "async run in thread" << ct->name() << ct->id();
},
[]() {
logNotice() << "result without value, run in thread" << ct->name() << ct->id();
});
AsyncFw::ThreadPool::async(
[]() {
std::this_thread::sleep_for(std::chrono::milliseconds(150));
logInfo() << "async run in thread" << ct->name() << ct->id();
return 1;
},
[](int r) {
logNotice() << "result:" << r << "run in thread" << ct->name() << ct->id();
AsyncFw::MainThread::exit(0);
});
AsyncFw::AbstractThreadPool::Thread *_t1 = AsyncFw::ThreadPool::instance()->createThread("DestroyFromThreadExample");
_t1->invokeMethod([_t1]() { _t1->destroy(); });
logNotice() << "Start Applicaiton";
int ret = AsyncFw::MainThread::exec();
logNotice() << "End Applicaiton" << ret;
return ret;
}

Member Function Documentation

◆ removeThread()

void ThreadPool::removeThread ( AbstractThreadPool::Thread * thread)
overridevirtual

Reimplemented from AsyncFw::AbstractThreadPool.

◆ quit()

void ThreadPool::quit ( )
overridevirtual

Reimplemented from AsyncFw::AbstractThreadPool.


The documentation for this class was generated from the following files: