Boost logo

Boost Users :

Subject: Re: [Boost-users] Passing io_service to socket in implementation file after initialization in header file
From: Richard Hodges (hodges.r_at_[hidden])
Date: 2018-06-18 15:15:39


A note of caution.

In all but the most exceptional of cases, you will want one io_service per
application.

Rather than tie the entire application to one instance of a class, which
might make testing difficult, you may want to consider providing the
io_service to the Foo as a dependency injection, with its lifetime
controlled by main().

Example (including the fixed constructor):

#include <boost/asio.hpp>

class foo
{
public:

    foo(boost::asio::io_service& ios); // Constructor.

private:

    boost::asio::io_service& ios;

    boost::asio::ip::udp::socket sock;
};

foo::foo(boost::asio::io_service& ios)
: ios(ios)
, sock(ios)
{
}

int main()
{
    boost::asio::io_service myios;

    foo f1(myios); // note - io_service is injected
    foo f2(myios);

    //... generate events etc

    myios.run();

    // now destroy foos and lastly, myios
}

On Mon, 18 Jun 2018 at 15:46, Vinnie Falco via Boost-users <
boost-users_at_[hidden]> wrote:

> On Mon, Jun 18, 2018 at 6:13 AM Álvaro Cebrián Juan via Boost-users
> <boost-users_at_[hidden]> wrote:
> > Why doing sock(ios); doesn't work?
>
> What is the complete error message from the compiler?
>
> Thanks
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net