Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need explanation for this boost::asio timer example

There is a line in the 3rd tutorial on Boost asio that shows how to renew a timer and yet prevent there from being drift. The line is the following:

 t->expires_at(t->expires_at() + boost::posix_time::seconds(1));

Maybe it's me but I wasn't able to find documentation on the 2nd usage of expires_at(), with no parameters. expires_at(x) sets the new expiration, cancelling any pending completion handlers. So presumably expires_at() does what, return time of the last expiry? So by adding one second, if there should be some number of ms, say n ms, then it will in essence be "subtracted" from the next expiry since the time is being accounted for? What happens then if the time it takes to perform this handler is greater than 1 second in this example? Does it fire immediately?

like image 962
ApplePieIsGood Avatar asked Nov 06 '22 15:11

ApplePieIsGood


1 Answers

expires_at() return the time when it is set to timeout. So this will move the timeout to 1 second later.

When you set the time with expires_at(x) you will get a return of 0 if it already invoked due to time already passed. If return is bigger then 0 it indicates number of cancels that were made.

like image 138
jpyllman Avatar answered Nov 12 '22 17:11

jpyllman