AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
ThreadPool/main.cpp

ThreadPool example

/*
Copyright (c) 2026 Alexandr Kuzmuk
This file is part of the AsyncFw project. Licensed under the MIT License.
See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
*/
#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(10));
logInfo() << "sync run in thread" << ct->name() << ct->id();
});
AsyncFw::ThreadPool::async([]() {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
logInfo() << "async run in thread" << ct->name() << ct->id();
});
AsyncFw::ThreadPool::async(
[]() {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
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(15));
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;
}
The AbstractThread class provides the base functionality for thread management.
Definition AbstractThread.h:45
std::string name() const
Returns name of managed thread.
Definition AbstractThread.cpp:679
std::thread::id id() const
Returns unique identifier of managed thread.
Definition AbstractThread.cpp:677
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:79
static AbstractThread * currentThread()
Returns a pointer to the AsyncFw::AbstractThread that manages the currently executing thread.
Definition AbstractThread.cpp:297
Управляет набором многократно используемых рабочих потоков для параллельного выполнения задач,...
Definition ThreadPool.h:52