2017-04-12 19:17 GMT+08:00 jupiter via Boost-users <boost-users@lists.boost.org>:

My program needs a sync udp socket send / receiver so I use the deadline_timer as timeout for udp socket receive_from on Debian Jessie:

boost::asio::deadline_timer deadline(io_service, boost::posix_time::seconds(2));

In checkDeadline function, when the timer expires, it calls socket.cancel, otherwise calls the deadline.async_wait(checkDeadline); again.

Apparently when the program runs to socket.receive_from(), it is blocked and the timer is never called because both socket.receive_from and the timer in the same thread.

I've been thinking to run the timer in another thread, but I checked Internet, there are comments said deadline_timer is not thread safe, please correct me if I am wrong here. It will be a simple solution for me if I can run timer on another thread. Anyone has other ideas to run the timer effectively?

Unfortunately, asio timers are useless with those blocking calls, you have to use the async version (e.g. async_receive_from) and code in async fashion instead.