Threads vs. Async

user277465 picture user277465 · Oct 26, 2010 · Viewed 60k times · Source

I have been reading up on the threaded model of programming versus the asynchronous model from this really good article. http://krondo.com/blog/?p=1209

However, the article mentions the following points.

  1. An async program will simply outperform a sync program by switching between tasks whenever there is a I/O.
  2. Threads are managed by the operating system.

I remember reading that threads are managed by the operating system by moving around TCBs between the Ready-Queue and the Waiting-Queue(amongst other queues). In this case, threads don't waste time on waiting either do they?

In light of the above mentioned, what are the advantages of async programs over threaded programs?

Answer

doron picture doron · Oct 26, 2010
  1. It is very difficult to write code that is thread safe. With asyncronous code, you know exactly where the code will shift from one task to the next and race conditions are therefore much harder to come by.
  2. Threads consume a fair amount of data since each thread needs to have its own stack. With async code, all the code shares the same stack and the stack is kept small due to continuously unwinding the stack between tasks.
  3. Threads are OS structures and are therefore more memory for the platform to support. There is no such problem with asynchronous tasks.