asynchronous vs non-blocking
http://stackoverflow.com/questions/2625493/asynchronous-vs-non-blocking
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
or poll
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:
- Blocking and synchronous mean the same thing: you call the API, it hangs up the thread until it has some kind of answer and returns it to you.
- Non-blocking means that if an answer can't be returned rapidly, the API returns immediately with an error and does nothing else. So there must be some related way to query whether the API is ready to be called (that is, to simulate a wait in an efficient way, to avoid manual polling in a tight loop).
- Asynchronous means that the API always returns immediately, having started a "background" effort to fulfil your request, so there must be some related way to obtain the result.
Asynchronous Asynchronous literally means not synchronous. Email is asynchronous. You send a mail, you don't expect to get a response NOW. But it is not non-blocking. Essentially本质上 what it means is an architecture where "components" send messages to each other without expecting a response immediately. HTTP requests are synchronous. Send a request and get a response.
Non-Blocking This term is mostly used with IO. What this means is that when you make a system call, it will return immediately with whatever result it has without putting your thread to sleep (with high probability). For example non-blocking read/write calls return with whatever they can do and expect caller to execute the call again. try_lock for example is non-blocking call. It will lock only if lock can be acquired. Usual semantics语义 for systems calls is blocking. read will wait until it has some data and put calling thread to sleep.
Event-base This term comes from libevent. non-blocking read/write calls in themselves are useless because they don't tell you "when" should you call them back (retry).
select/epoll/IOCompletionPort etc are different mechanisms for finding out from OS "when" these calls are expected to return "interesting" data.
libevent and other such libraries provide wrappers over these event monitoring facilities provided by various OSes and give a consistent API to work with which runs across operating systems.
Non-blocking IO goes hand in hand with Event-base.
I think these terms overlap.
For example HTTP protocol is synchronous but HTTP implementation using non-blocking IO can be asynchronous.
Again a non-blocking API call like read/write/try_lock is synchronous (it immediately gives a response) but "data handling" is asynchronous.
相关文章
- 跨语言和跨编译器的那些坑(CPython vs IronPython)
- [Angular 2] Angular 2 Smart Components vs Presentation Components
- Firefly 本地签出 关闭VS 删除本地代码后 在firefly无法删除相应目录
- SAP CRM WebClient UI WD_CREATE_CONTEXT VS DO_INIT_CONTEXT
- 你眼中的程序员 VS 程序员眼中的自己,是时候打破代沟了
- 我学的是设计模式的视频教程——辛格尔顿,生成器VS工厂方法
- 解决VS编辑器报 "无法将类型A强制转换为类型B"的控件问题
- 【程序人生】“进大厂大半年,每个月都想走,”大公司 VS 小公司到底该如何选择?
- scichart 6.3.X Crack [vs] LightningChart
- VS code创建虚拟环境并安装Python包
- Haproxy 超详细总结 四层负载均衡VS七层负载均衡 LVS VS Haproxy VS Nginx