1

I heard from a person that he chooses asynchronous IO instead of synchronized IO in the design of his system, because there will be less threads and threads are expensive.

But isn't asynchronous IO's advantage is to release the CPU from waiting for the IO to finish and do something else and then being interrupted by the IO operation once it finishes running. Does that change the number of threads or processes? I don't know.

Thanks.

usr
  • 168,620
  • 35
  • 240
  • 369
Tim
  • 1
  • 141
  • 372
  • 590
  • 1
    Voted to close. What OS? What IO, disk or network? What language/framework? The answer depend heavily on those details. – Soonts Jun 07 '16 at 22:25
  • 1
    The short answer — it depends on how asynchronous IO is implemented in the programming language, its runtime, the OS, and the drivers. – Soonts Jun 07 '16 at 22:29

3 Answers3

1

The CPU is released in any case. It does not make sense to keep a CPU core idle while an IO is in progress. Another thread can be scheduled.

Async IO really is about having less threads. An IO is just a kernel data structure. An IO does not need to be "on a thread". Async IO decouples threads and IOs.

If you have 1M connected sockets and are reading on all of them you really don't want 1M threads.

usr
  • 168,620
  • 35
  • 240
  • 369
0

I suggest you read this https://stackoverflow.com/a/14797071/2241168 . It's practical example to illustrate the difference between async and sync model.

Community
  • 1
  • 1
GustavoIP
  • 873
  • 2
  • 8
  • 25
0

The root of the confusion is that you actually have three options:

  • In a single thread, start I/O and wait for it to finish ("blocking" or "synchronous" I/O)
  • In a single thread, start I/O and do something else while it is running ("asynchronous" I/O)
  • In multiple threads, start I/O and "wait" for it to finish ("blocking" or "synchronous" I/O, but with multiple threads). In this case only one thread waits while the others go on doing useful work.

So if you use async I/O, you are avoiding both alternatives -- blocking the CPU or using multiple threads.

Martin Geisse
  • 1,189
  • 1
  • 9
  • 22