Re: [asio-users] using a demuxer to poll
Brought to you by:
chris_kohlhoff
From: Christopher K. <ch...@ko...> - 2005-09-09 02:54:29
|
Hi Arvid, --- Arvid Norberg <c9...@cs...> wrote: > I want to use one of my demuxers to poll the messages received on any > of its sockets. i.e. have the same semantics as calling select() with > a timeout of 0. I'm doing it like this: > > asio::deadline_timer t(m_demuxer, milliseconds(0)); > t.async_wait(bind(&asio::demuxer::interrupt, ref(m_demuxer))); > m_demuxer.run(); > m_demuxer.reset(); > > Does this guarantee that all pending socket events are handled before > the timer event is handled and the demuxer is aborted? No, the run will be interrupted as soon as possible, even if there are handlers ready to be called. > If, not is > there a better way of achieving what I want? If you are just waiting for data to be received on the socket, you could use the stream_socket's in_avail() function to check if there is data waiting. This function will not block, and does not require you to run a demuxer at all. However polling is generally not a scalable approach. Could you explain the larger design problem you are trying to solve? Perhaps there's another approach. > And another question, is there a reason why run doesn't call reset() > before it quits? are there any operations in the demuxer that makes > sense only when it's in the state of "just returned from run, but not > reset"? or is the reset() operation expensive so one may want to skip > it if run() is not to be called again? This is because run() can be called from multiple threads simultaneously. You can't reset the demuxer until after the last run() function has exited, therefore it is kept separate. E.g.: demuxer d; thread th1(bind(&demuxer::run, &d)); thread th2(bind(&demuxer::run, &d)); thread th3(bind(&demuxer::run, &d)); // ... th1.join(); th2.join(); th3.join(); d.reset(); Cheers, Chris |