C++ (Qt)#include <boost/asio.hpp>#include <boost/chrono.hpp>#include <iostream> using namespace std;using namespace boost::asio; void onTimeout1( const boost::system::error_code &ec, deadline_timer &timer ){ if( ec == error::operation_aborted ) return; cout << "Timer1 triggered" << endl; timer.expires_from_now( boost::posix_time::seconds( 3 ) ); timer.async_wait( [&timer]( const boost::system::error_code &ec ){ onTimeout1( ec, timer ); } );} void onTimeout2( const boost::system::error_code &ec, deadline_timer &timer ){ if( ec == error::operation_aborted ) return; cout << "Timer2 triggered" << endl; timer.expires_from_now( boost::posix_time::seconds( 10 ) ); timer.async_wait( [&timer]( const boost::system::error_code &ec ){ onTimeout2( ec, timer ); } );} int main(){ cout << "Run demo" << endl; io_context io; deadline_timer timer1( io ); deadline_timer timer2( io ); onTimeout1( boost::system::error_code(), timer1 ); onTimeout2( boost::system::error_code(), timer2 ); io.run(); return 0;}
m_timerID = this->startTimer();
auto future = run(foo, arg1, arg2).then([](auto future2) {return run(bar, future2.get());}).then([](auto future3) { return run(baz, future3.get()); });co_wait future;
// future::wait#include <iostream> // std::cout#include <future> // std::async, std::future#include <chrono> // std::chrono::milliseconds// a non-optimized way of checking for prime numbers:bool is_prime (int x) { for (int i=2; i<x; ++i) if (x%i==0) return false; return true;}int main (){ // call function asynchronously: std::future<bool> fut = std::async (is_prime,194232491); std::cout << "checking...\n"; fut.wait(); std::cout << "\n194232491 "; if (fut.get()) // guaranteed to be ready (and not block) after wait returns std::cout << "is prime.\n"; else std::cout << "is not prime.\n"; return 0;}