1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <boost/asio.hpp>
#include <iostream>
#include <atomic>
int main() {
std::atomic<bool> shouldStop = false;
boost::asio::io_service ios;
boost::asio::deadline_timer timer(ios);
// expires dans 5 sec
timer.expires_from_now(boost::posix_time::seconds(5));
// lambda exécutée quand le timer expire (on met shouldStop à true quand il expire)
timer.async_wait([&shouldStop](const boost::system::error_code& error) {
if(!error) {
shouldStop = true;
}
});
// on effectue le traitement, en attendant que le timer expire
while(!shouldStop) {
// Do something
std::cout << "Hi !" << std::endl;
}
return 0;
} |
Partager