What is the difference between asynchronous and non-blocking calls? Also between blocking and synchronous calls (with examples please)?
Asynchronous and non-blocking calls? also between blocking and synchronous
asynchronousblockingsynchronous
Related Topic
- What’s the difference between: Asynchronous, Non-Blocking, Event-Base architectures
- Node.js – non-blocking or asynchronous I/O in Node.js
- Javascript – the difference between synchronous and asynchronous programming (in node.js)
- Javascript – Why is the variable unaltered after I modify it inside of a function? – Asynchronous code reference
- C# – the difference between asynchronous programming and multithreading
Best Answer
In many circumstances they are different names for the same thing, but in some contexts they are quite different. So it depends. Terminology is not applied in a totally consistent way across the whole software industry.
For example in the classic sockets API, a non-blocking socket is one that simply returns immediately with a special "would block" error message, whereas a blocking socket would have blocked. You have to use a separate function such as
select
orpoll
to find out when is a good time to retry.But asynchronous sockets (as supported by Windows sockets), or the asynchronous IO pattern used in .NET, are more convenient. You call a method to start an operation, and the framework calls you back when it's done. Even here, there are basic differences. Asynchronous Win32 sockets "marshal" their results onto a specific GUI thread by passing Window messages, whereas .NET asynchronous IO is free-threaded (you don't know what thread your callback will be called on).
So they don't always mean the same thing. To distil the socket example, we could say: