Avoiding busy loop with boost::asio::io_service run

2.7k Views Asked by At

I am implementing some asio operations using boost, I've encountered an interface problem, whom which I am not receiving 'handlers' in initialization, but right after,

This forces me to write a 'busy' loop, what I'd like to do is have the io_service run even without having at least 1 handler, is it possible? what is the way to handle this? do a wait on handlers on the service? this is my code..

    /** : */
    void                Run             () { while(true) {m_srv.run(); Sleep(1);} } // once 1 handler is inside service, thread will not be in busy loop
private: // members:

    io_service  m_srv;

Any suggestions? thanks

This is the code problem: (m_drv is a task that operates boost::thread(io_service::run..))

class App : public Base::Application
{
public:
    /** : */
    App(char* name, char* id) : Application(name, id), m_drv("NetTask"), m_worker("Worker"), m_acceptor(m_worker, Drv(), &OnAccept, 4567)
    {
        m_acceptor.Accept(Drv());
    }

    /** : */
    inline DriverImp& Drv() { return static_cast<DriverImp&>(m_drv.Imp());}

    /** : */
    inline Reactor& Worker() { return m_worker; }
public: 
    APTR(Socket) m_sock;
private: // members:
    Driver      m_drv;
    Reactor     m_worker;
    Acceptor    m_acceptor;
};
1

There are 1 best solutions below

9
On BEST ANSWER

You need to use io_service::work:

boost::asio::io_service        service;
boost::asio::io_service::work  work( service );

Note: in destructor the work object notifies the service that the work is complete and io_service::run() may return.