Sometime, we want to execute a task asynchronously at specified time. For example, add random latency to incoming messages in the development of simulator, or cancel an order unsolicitedly when its end time arrives in the development of OMS. Check out boost manual about Using a timer asynchronously.
using boost::asio::deadline_timer;
struct TaskPoolTimer : public TaskPool
{
template
void run(T func, uint64_t us)
{
auto t = new deadline_timer(service_, boost::posix_time::microseconds(us));
tt->async_wait([=](const boost::system::error_code&) {
func(); delete tt;
});
}
};
int main() {
TaskPoolTimer p;
std::cout << time(NULL) << std::endl;
auto f = [](){std::cout<< time(NULL) << std::endl;};
p.run(f, 2e+6);
p.run(f, 4e+6);
return 0;
}