博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asynchronous vs non-blocking
阅读量:5157 次
发布时间:2019-06-13

本文共 3490 字,大约阅读时间需要 11 分钟。

 

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.

 

转载于:https://www.cnblogs.com/chucklu/p/5816807.html

你可能感兴趣的文章
MySQL面试题中:主从同步的原理
查看>>
项目练习(二)—微博数据结构化
查看>>
WebService原理
查看>>
HDU - 1032 The 3n + 1 problem
查看>>
创建自己的Convers. Routine.
查看>>
如何解压缩.7z 001,.7z002....
查看>>
创建一个广播机制!
查看>>
HDFS使用流的方式上传下载
查看>>
iOS高级-QuartzCore框架-背景平铺
查看>>
惊群24小时
查看>>
Linux SSH免密码登录
查看>>
kafka + storm 错误 Async loop died
查看>>
微信小程序 开发文档
查看>>
《玩转D语言系列》一、通过四个版本的 Hello Word 初识D语言
查看>>
破译实践
查看>>
星星评分
查看>>
validform 一行代码完成所有验证
查看>>
工作中上的一点思考
查看>>
bzoj 2440: [中山市选2011]完全平方数【莫比乌斯函数+二分】
查看>>
day17 appium环境搭建
查看>>