How to replace get_io_service()/get_io_context()-calls

Hello, I have been using boost::asio for about a decade. I was using boost-1_63. I use get_io_service() (e.g. on a socket) to get a reference to the io_context, e.g. to post(…) on that io_context. I have read, that get_io_service() got deprecated and was removed. I now am facing, the challenge to find a replacement for get_io_service(). I found https://stackoverflow.com/a/39447000/6832488 suggesting to use get_executor().context() as a replacement for get_io_service(). And although the reference returned from get_executor().context() points to the same address as my io_context, it does have the wrong type. I cannot do socket.get_executor().context().post(…). I also found https://stackoverflow.com/a/67773642/6832488, suggesting to (C-Style)cast s to (boost::asio::io_context&).
#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context()) But trying C++ (more or less) type safe to static_cast< boost::asio::io_context&>(s) fails to compile.
I found that I misinterpreted
((boost::asio::io_context&)(s).get_executor().context()) to cast (s) to ((boost::asio::io_context&). When I use static_cast<boost::asio::io_service&>(s.get_executor().context()) My code finally compiles.
I must admit, that I find the documentation of get_executor() quiet useless. Btw, Boost.org seems to be useless, too. When I try to read https://www.boost.org/doc/libs/1_63_0/doc/html/boost_asio/reference.html, I get
Error 503 first byte timeout. ☹
The documentation of basic_socket::get_io_service() in version 1_40_0 lists its signature as:
boost::asio::io_service & get_io_service();
That is useful, I can go to the documentation of boost::asio::io_service and find the methods usable on the io_service like post(). But for the current boost version 1_88_0, the signature of basic_stream_socket::get_executor() is documented as:
const executor_type & get_executor();
Ok, executor_type is documented as of basic_stream_socket::executor_type
typedef Executor executor_type;
Executor is a template parameter. But when I look at ip::tcp::socket, I find:
typedef basic_stream_socket< tcp > socket;
But basic_stream_socket is documented as
template< typename Protocol, typename Executor> class basic_stream_socket : public basic_socket< Protocol, Executor >
Where the hell is the 2nd template parameter for basic_stream_socket? There is no default value of Executor, so how does the template instantiation of basic_stream_socket<tcp> compile? How am I supposed to find out, that I can invoke context() on the return value of get_executor()? I am totally screwed up with the documentation of current boost::asio. ☹ I hope, anybody on this mailing list can enlighten me. Many thanks in advance, Mario Klebsch Mario Funktion | R&D Tel: +49 (0) 531 38 701 718 Raum: Braunschweig, E20 Diese E-Mail und die an sie angehängten Dateien sind ausschließlich für Personen oder Institutionen bestimmt, deren Namen oben aufgeführt sind. Sie können Informationen enthalten, die durch das Berufsgeheimnis geschützt sind und deren Weitergabe strengstens untersagt ist. Jede elektronische Nachricht kann geändert werden. ACTIA lehnt jede Verantwortung für diese Nachricht ab. Der Inhalt dieser Nachricht stellt keine Verpflichtung seitens unseres Unternehmens dar. Wenn Sie kein Empfänger sind, weisen wir Sie darauf hin, dass das Lesen, Vervielfältigen oder Verteilen strengstens untersagt ist. Wir bitten Sie daher, uns umgehend über diesen Brief zu informieren und diese Nachricht sowie eventuell beigefügte Unterlagen aus Ihrem Postfach zu löschen. Danke. This e-mail and the files attached to it are intended exclusively for persons or institutions whose names are listed above. They may contain information that is protected by professional secrecy and the disclosure of which is strictly prohibited. Any electronic message can be modified. ACTIA declines all responsibility for this message. The content of this message does not represent a commitment on the part of our company. If you are not a recipient, we would like to point out that reading, copying or distribution is strictly prohibited. We therefore ask you to inform us immediately about this letter and to delete this message and any accompanying documents from your mailbox. Thank you.

On Wed, May 21, 2025 at 7:52 PM Klebsch, Mario via Boost-users <boost-users@lists.boost.org> wrote:
There is no default value of Executor, so how does the template instantiation of basic_stream_socket<tcp> compile?
The default executor is specified a few lines above in the forward declaration: template <typename Protocol, typename Executor = any_io_executor> class basic_stream_socket;
I cannot do socket.get_executor().context().post(…).
You don't need the context. You can write it like: asio::post(socket.get_executor(), []() {}); Asio has changed the default executor of io_object types from io_context::executor_type to any_io_executor, to make them easier to use and reduce code bloat as there is no need for generating special composed operations for each executor type. You can find that in the change log: https://www.boost.org/doc/libs/develop/doc/html/boost_asio/history.html#boos... You can still use a concrete executor type if you want. It will of course change the socket type: using my_socket_t = typename asio::ip::tcp::socket::rebind_executor<asio::io_context::executor_type>::other; You can also use any_io_executor and access the concrete executor when you know the underlying executor type: asio::any_io_executor executor{ io_context }; executor.target<asio::io_context::executor_type>().something...
participants (2)
-
Klebsch, Mario
-
Mohammad Nejati [ashtum]