Proactor and async write

2019-07-05 06:01发布

问题:

Boost asio implements proactor design pattern baded on ACE proactor.

I understand why we need async read. Hovewer, I'm a confused with async write.

  1. Why we need is async write? Is it useful for TCP/UDP connection too (can write to TCP/UDP socket take time)?
  2. Can I mix async read with sync write?

回答1:

1) Why we need is async write? Is it useful for TCP/UDP connection too (can write to TCP/UDP socket take time)?

Asynchronous write is needed for the very same reasons as asynchronous read. When using synchronous write operations, the calls block until all data has been transmitted. This is not desirable for a number of reasons. Primarily to achieve concurrency without use of explicit threads, this is the basis of the proactor design pattern.

2) Can I mix async read with sync write?

Yes, they can and should be mixed. It would be a very odd design to use asynchronous read operations, yet synchronous write operations.