0

I have a long lived process that will get through asio from a variety of sources such as a websocket connection. If these sources were going to just send data once then my process could just call async_read the correct number of times then call ios.run() to wait for all those reads to finish. However, my process is long lived

user782220
  • 10,677
  • 21
  • 72
  • 135
  • You can *still* use `async_read`. Since they are *asynchronous* you can have many pending such operations, and the ASIO event loop will do the polling and execution as needed. If you're worried about the ASIO event loop blocking your main thread, then put it in its own thread? – Some programmer dude Nov 13 '17 at 05:02
  • Yes I know I can use async_read but eventually ios.run() will return and the program will exit. How do I get the ios to wait forever for new input? – user782220 Nov 13 '17 at 06:41
  • Call `ios.run()` in a loop, while the service is not stopped. – Some programmer dude Nov 13 '17 at 06:43
  • That is basically a while loop polling. The whole point of epoll is to avoid burning cpu cycles in a loop. – user782220 Nov 13 '17 at 09:06
  • @user782220 see io_service::work (I'll find a suitable duplicate). This is indeed the whole point of async io and Asio. – sehe Nov 13 '17 at 12:05
  • Integrating Asio with an existing event loop: [`run()` vs. `poll()`](https://stackoverflow.com/questions/4705411/boostasio-io-service-run-vs-poll-or-how-do-i-integrate-boostasio-in-ma) and e.g. [https://stackoverflow.com/questions/1001032/how-to-integrate-boost-asio-main-loop-in-gui-framework-like-qt4-or-gtk](https://stackoverflow.com/questions/1001032/how-to-integrate-boost-asio-main-loop-in-gui-framework-like-qt4-or-gtk) – sehe Nov 13 '17 at 12:08

0 Answers0