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

Invoke method 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/Log>
int main(int argc, char *argv[]) {
AsyncFw::Thread thread1("T1");
AsyncFw::Thread thread2("T2");
thread1.start();
thread2.start();
logDebug() << "Main id:" << _mainThread->id();
logDebug() << "T1 id:" << thread1.id();
logDebug() << "T2 id:" << thread2.id();
thread1.invokeMethod([&thread2, _mainThread]() {
logInfo() << "run in thread" << ct->name() << ct->id();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
thread2.invokeMethod([_mainThread]() {
logInfo() << "run in thread" << ct->name() << ct->id();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
_mainThread->invokeMethod([_mainThread]() {
logInfo() << "run in thread" << ct->name() << ct->id();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
logInfo() << "exit application";
AsyncFw::MainThread::exit(0);
});
});
});
logNotice() << "Start Applicaiton";
int ret = AsyncFw::MainThread::exec();
logNotice() << "End Applicaiton";
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
void start()
Create a managed thread and run exec().
Definition AbstractThread.cpp:642
AsyncFw::Thread thread with sockets.
Definition Thread.h:16