async vs multithreaded

This content has 6 years. Please, read this page keeping its age in your mind.

Many times, many people get confused about the difference. I was looking to set up a TCP server using Boost (of course). There is a tutorial by Mahreen Tahir saying that:

But what if there are more than one clients trying to connect to the server? In case we don’t want our main program to be interrupted while we’re reading from or writing to a socket, a multi-threaded TCP client-server is required to handle the situation. The other option is having an asynchronous server.

So, let’s understand the difference, using a great example by Eric Lippert:

  • Synchronous: you cook the eggs, then you cook the toast.
  • Asynchronous, single threaded: you start the eggs cooking and set a timer. You start the toast cooking, and set a timer. While they are both cooking, you clean the kitchen. When the timers go off you take the eggs off the heat and the toast out of the toaster and serve them.
  • Asynchronous, multithreaded: you hire two more cooks, one to cook eggs and one to cook toast. Now you have the problem of coordinating the cooks so that they do not conflict with each other in the kitchen when sharing resources. And you have to pay them.

So, now you know what you want…don’t you?