Question

  1. io_service::run() is called by thread A. Is it safe to call async_write from thread B?

  2. io_service::run() is called by thread A. Are async operations executed by thread A, or is thread A only guaranteed to call handlers and behind the scenes there could be additional threads that execute the operations?

  3. io_service::run() is called by thread A. Some thread calls async_read and async_write using the same buffer. Is it safe to assume that the buffer will be accessed by at most one operation at a time? Or is it so that only handlers are called serially, but behind the scenes reads and writes can occur simultaneously?

  4. The documentation says "The program must ensure that the stream performs no other read operations (such as async_read, the stream's async_read_some function, or any other composed operations that perform reads) until this operation completes.". Is it correct to interpret this as "You must not perform more than one read operation on a socket at a time. But you may perform 10 read operations on 10 distinct sockets."?

  5. Having a socket that indefinitely accepts data, is it a good idea to call async_read and call it again from async_read's handler?

  6. Does io_service::stop() stop all pending async operations or simply stops accepting new ones and executes the pending ones?

Was it helpful?

Solution

  1. Yes, providing the io_service is tied to whatever is calling async_write. However, it should be noted that it is safe to call async_write from thread B even if the run is not called: it'll get queued in the io_service and wait until one of the run-ing calls are completed.
  2. The callbacks posted to the io_service will run on thread A. Other async operations (such as timer operations) can happen on other threads. What is guarenteed to be on A and what is on its own thread is defined by the specific object being used, not by io_service.
  3. Nope. Yup-ish. Depends on the class calling io_service.
  4. Yes.
  5. Yes, in fact this is super common, as it both ensures that only 1 async_read call is running at a time for a given socket and that there is always "work" for the io_service.
  6. It usually finished the last callback and then stops accepting new ones and stops processing pending ones. It actually still accepts new ones but forces a reset is called before any other callbacks are called.

io_service is a message queue (basically), while a socket that posts its messages to the io_service is something else entirely.

OTHER TIPS

1: Yes

4: Yes, it's okay to perform distinct operations on distinct sockets.

5: Yes, if you check the examples that's how they do it.

6: Considering the reference manual says

All invocations of its run() or run_one() member functions should return as soon as possible.

I would say it might do any.


For number 2 and 6, the source is available so the best way to answer those question is by downloading and reading it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top