Reputation: 395
If I already have boost's ios service running in a separate thread like this:
boost::asio::io_service ios;
boost::thread bt(boost::bind(&boost::asio::io_service::run, &ios));
Is it possible to then use it for a socket or timer? Or does it need to be stopped before it is passed to these constructors?
boost::asio::deadline_timer dt1 = boost::asio::deadline_timer(ios);
Upvotes: 0
Views: 562
Reputation: 393134
io_service
is threadsafe, it does not need to be stopped in order to post (more) work.
Of course, you have to make sure it is still running and keeps running as long as you want to be able to post additional work.
ioservice::work
exists for that puppose.
Upvotes: 0
Reputation: 2973
Yes, it has to be running for async io to work - see this answer for detailed explanation. The problem is io_service::run method returns when there is no work to do and i/o thread may just exit. The standard solution is to create io_service::work instance before starting i/o thread. Also you maybe would like to catch exceptions in background thread to prevent i/o loop termination.
Upvotes: 1