Endophage
Endophage

Reputation: 21473

Boost asio http client - io_service.run()

I'm trying to work out how the io_service.run() function operates in a client. This is somewhat related to this question but on the other end of the connection. In a client situation there is no accept handler so when will the io_service.run() return?

Ideally I want to run it once (per thread, as I understand it, correction welcome, each thread that calls io_service.run() is basically added to the io_service thread pool) then send lots of requests without needing to call reset, run again. There will be gaps in the requests being sent when the io_service will have no work to do but the occurrence of those gaps is largely non-deterministic, based on user behavior.

Upvotes: 1

Views: 881

Answers (1)

Ralf
Ralf

Reputation: 9573

As you stated, an io_service returns when it runs out of work e.g. in a server the accept call provides it with work. To prevent an io_service from running out of work, have a look at the io_service::work class.

This link taken from this SO post, provides an overview of how the work object can be used. In particular storing the work object in a shared_ptr and resetting the shared_ptr when wanting to stop the application, etc. is a useful idiom. By not calling the io_service::stop() method (which can also be used to stop the io_service) you allow all required work to be completed cleanly.

Upvotes: 2

Related Questions