boost::asio - reason for asio_handler_invoke
Hi, I've been trying to understand the reasons to have asio_handler_invoke function, and so far I am unable to understand why it is needed. It is used to override invocation policy for a function-like object based on the handler. boost::asio gives one example with using io_service::strand: template<typename F> void asio_handler_invoke(F fun, MyHandler* h) { h->strand.invoke(fun); } My question is why is it needed to make this function templatized on ANY function object, and not just MyHandler? Why can't we just define MyHandler::operator() in terms of strand like this: struct MyHandler { io_service::strand strand. void operator()() { strand.invoke(boost::bind(&MyHandler::private_invoke, this)); } void private_invoke() { // do stuff... } } Thanks for any help. -- View this message in context: http://www.nabble.com/boost%3A%3Aasio---reason-for-asio_handler_invoke-tp146... Sent from the Boost - Users mailing list archive at Nabble.com.
mikpol wrote:
I've been trying to understand the reasons to have asio_handler_invoke function, and so far I am unable to understand why it is needed. It is used to override invocation policy for a function-like object based on the handler. boost::asio gives one example with using io_service::strand...
I have finally understood the reason for asio_handler_invoke after reading asio TR2 proposal carefully. It is due to the bound handlers invoked by the library for functions such as async_read and async_read_until. They need to call read function on the socket multiple times, and a handler created by the library is needed to be called in the same way as the user provided handler. I knew about this before, but I couldn't understand what was shared between those handler's that needs to be protected from simultaneous invocation by multiple threads. It turns out this is the socket object itself, which is referenced from all these handlers and needs protection from potential concurrent invocation. If anyone finds a problem in this explanation or would like to comment on other reasons why it is needed, please post something, I would be interested in hearing your opinion. Thanks. -- View this message in context: http://www.nabble.com/boost%3A%3Aasio---reason-for-asio_handler_invoke-tp146... Sent from the Boost - Users mailing list archive at Nabble.com.
Would be nice to have explanation like this sitting in the documentation
too... There is no such thing as too much documentation, right :) ?
On Tue, 08 Jan 2008 07:44:33 -0700, mikpol
mikpol wrote:
I've been trying to understand the reasons to have asio_handler_invoke function, and so far I am unable to understand why it is needed. It is used to override invocation policy for a function-like object based on the handler. boost::asio gives one example with using io_service::strand...
I have finally understood the reason for asio_handler_invoke after reading asio TR2 proposal carefully. It is due to the bound handlers invoked by the library for functions such as async_read and async_read_until. They need to call read function on the socket multiple times, and a handler created by the library is needed to be called in the same way as the user provided handler. I knew about this before, but I couldn't understand what was shared between those handler's that needs to be protected from simultaneous invocation by multiple threads. It turns out this is the socket object itself, which is referenced from all these handlers and needs protection from potential concurrent invocation.
If anyone finds a problem in this explanation or would like to comment on other reasons why it is needed, please post something, I would be interested in hearing your opinion.
Thanks.
participants (2)
-
Andrey Tcherepanov
-
mikpol