[boost-users][asio] timeout - is io_service.reset() safe with pool of threads?
hello, I'm implementing timeouts according to suggestion placed here http://lists.boost.org/Archives/boost/2007/04/120339.php because I still don't understand fully how io_service work I have question: is this way of implementation of timeout safe when I'm using pool of threads for one io_service? tom ---------------------------------------------------- Kate Winslet i Leonardo DiCaprio znowu razem na ekranie! DROGA DO SZCZĘŚCIA - w kinach od 30 stycznia. Zobacz zwiastun: http://klik.wp.pl/?adr=http%3A%2F%2Fcorto.www.wp.pl%2Fas%2Fszczescie.html&sid=624
I'm implementing timeouts according to suggestion placed here http://lists.boost.org/Archives/boost/2007/04/120339.php
because I still don't understand fully how io_service work I have question:
is this way of implementation of timeout safe when I'm using pool of threads for one io_service?
That function assumes the io_service associated with your socket is *stopped*, so it cannot run either in one thread or in multiple threads. Note that it is an async. implementation for *synchronous* use, and when you use sync. i/o you do not explicitly run io_service: io_service io; tcp::socket sock(io); // open socket, define your buffer..., then: read_with_timeout(sock, buffer); But if you've already got an async. design, you don't need workarounds to implement timeouts - just use deadline_timer in the straightforward way.
You know, io_service must be run for accepting.
Again - io_service must be running for *asynchronous* accepting (async_accept), but if you use syncronous accept() then you never explicitly run io_service. See the following example: http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/example/echo/blocki...
don't we have to do it in such way?
acceptor_(io_service_) acceptor_.bind acceptor_.listen acceptor_.async_accept io_service_.run
You mixed here blocking and asyncronous calls (which is legitimate), and since asyncronous calls present in your code someone must process them - that's exactly what io_service::run does, and that's why you must call io_service_run() (please note that io_service.run() is blocking call, meaning that it wouldn't return before all pending work is done). But if you would stick with blocking calls only, you wouldn't have to run io_service: acceptor_(io_service_) acceptor_.bind acceptor_.listen acceptor_.accept // not async_accept! // do something with the accepted socket...
Note that it is an async. implementation for *synchronous* use, and when you use sync. i/o you do not explicitly run io_service: io_service io; tcp::socket sock(io); // open socket, define your buffer..., then: read_with_timeout(sock, buffer); I don't understend. what is synchronous here? could You explain Igor? "Synchronous" (or blocking) means that a function call completes ("returns") only after its job is done. In our case read_with_timeout blocks until the data is read or the timeout occurs. Asynchronous version would return immediately, and when the desired event occurs, it would notify you by means of the completion handler that you supply.
But if you've already got an async. design, you don't need workarounds to implement timeouts - just use deadline_timer in the straightforward way. it's mean - how? which example shows it?
Please, look at this exmaple (it's very simple and demonstrates exactly what you need): http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/example/timeouts/ac...
acceptor_(io_service_)
acceptor_.bind acceptor_.listen acceptor_.async_accept io_service_.run
You mixed here blocking and asyncronous calls (which is legitimate),
so, how 'pure' async_accept should looks than? (without mixing)
I'm sorry, I just missread your code - you don't mix anything there, you just set-up the acceptor and launch asynchronous accept.
at last I understood how io_service works. till now I thought "why they are talking that I have to schedule async operation first than run io_service. in server example there is async_accept and io_service.run() and server is accepting forever. So, run() must running forever. ". I did not caught that in async_accept handler there is once again async_accept. this trick don't let run() never end.
Exaclty. If you look at other asio examples you'll see the same pattern: async. operations are chained in the way that completion handler of a previous operation launches the next one. This pattern allows you to end the i/o in a very simple way: just break the above chain and the io_service.run() ends. Note also that if your handlers are made of some "connection" object (using shared_from_this, like in the examples) that encapsulates socket, then when the chain terminates, this object is automatically destroyed closing its socket. However, such a simple design is not always possible. In more complicated cases you have to keep io_service running *always*, even when there's no i/o activity - until explicitly stopped. Refer to the following example to see how you can do this: http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/example/http/server...
It seems that I understand now what does it mean : explicitly/implicitly run io_service, synchronous/*synchronous*
In a synchornous (blocking) design you cannot run io_service "message loop" (which would block itself), so blocking functions pump their io_service by themselvs, in their implementations.
once again thanks for your efforts (in sharing asio knowledge) Igor
you're welcome :)
Hi, Though I have not checked rigorously right within the boost, but wanted to get quick answer to the query below: “Do we have the complete implementation of the C++0x language feature: “concepts”: within boost ? Actually, conceptgcc is very slow compared to gcc, so I was looking for quick solution in order to use concepts in its entirety. Thanks, Chandra ********************************************************************** This communication contains information which is confidential and may also be legally privileged. It is for the exclusive use of the intended recipient(s). If you are not the intended recipient(s), disclosure, copying, distribution, or other use of, or action taken or omitted to be taken in reliance upon, this communication or the information in it is prohibited and maybe unlawful. If you have received this communication in error please notify the sender by return email, delete it from your system and destroy any copies. **********************************************************************
AMDG Chandra Kumar wrote:
Though I have not checked rigorously right within the boost, but wanted to get quick answer to the query below:
“Do we have the complete implementation of the C++0x language feature: “*concepts*”: within boost ?
No. Concepts really are a language feature, not a library feature. C++03 emulation for concepts is very imperfect (and has significantly different syntax).
Actually, conceptgcc is very slow compared to gcc, so I was looking for quick solution in order to use concepts in its entirety.
In Christ, Steven Watanabe
on Mon Feb 02 2009, "Chandra Kumar"
Hi,
Though I have not checked rigorously right within the boost, but wanted to get quick answer to the query below:
“Do we have the complete implementation of the C++0x language feature: “concepts”: within boost ?
The C++0x language feature is not implementable as a library in C++03. However, we do have http://www.boost.org/doc/libs/release/libs/concept_check/concept_check.htm
Actually, conceptgcc is very slow compared to gcc, so I was looking for quick solution in order to use concepts in its entirety.
ConceptGCC is your best bet until production implementations of the concepts features become available. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
I have understood it yesterday evening when I read about boos.shared_ptr but I still don't understand why connection object must be noncopyable...
You can make it copyable if you wish, but if the an object has an asio socket as a member, then it's non-copyable just because the socket object is non-copyable.
I don't understand how it works :(. we have few io_service object which are run() in separated threads. but only one is maintained by async_accept chain trick. the rest run() should immediately return.
Please, read that example carefully, there's an explanation inside. You've got special "work" object that prevents io_service::run from stopping.
but I don't know for what there is pool of threads in "http server 3" example. there is only one scheduled task - async_accept. So, in my opinion only one thread will be doing whole job.
First of all, there're more tasks: pay attention that before issuing new async_accept, the Server calls connection->open(), which in turn issues async_read etc - all this is processed by one single io_service... But anyway - what if you've got 1000 incoming connections simultaniously? If you'd got 1 thread, it would accept them one by one, processing in the meanwhile read/write operations of the Connection object.
participants (5)
-
Chandra Kumar
-
David Abrahams
-
Igor R
-
Steven Watanabe
-
tomasz jankowski