异步和非阻塞调用?也在阻塞和同步之间 [英] asynchronous and non-blocking calls? also between blocking and synchronous

查看:48
本文介绍了异步和非阻塞调用?也在阻塞和同步之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

异步调用和非阻塞调用有什么区别?还在阻塞调用和同步调用之间(请举例说明)?

What is the difference between asynchronous and non-blocking calls? Also between blocking and synchronous calls (with examples please)?

推荐答案

在许多情况下,它们是同一个事物的不同名称,但在某些上下文中,它们是完全不同的.所以这取决于.术语在整个软件行业中的应用方式并不完全一致.

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.

例如在经典的套接字 API 中,非阻塞套接字是简单地立即返回并带有特殊的将阻塞"错误消息的套接字,而阻塞套接字将被阻塞.您必须使用一个单独的函数,例如 selectpoll 来找出重试的好时机.

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.

但是异步套接字(由 Windows 套接字支持)或 .NET 中使用的异步 IO 模式更方便.你调用一个方法来启动一个操作,当它完成时框架会回调你.即使在这里,也存在基本差异.异步 Win32 套接字通过传递 Window 消息将其结果编组"到特定的 GUI 线程上,而 .NET 异步 IO 是自由线程的(您不知道将在哪个线程上调用您的回调).

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:

  • 阻塞和同步意味着同一件事:您调用 API,它会挂起线程,直到它得到某种答案并将其返回给您.
  • 非阻塞意味着如果无法快速返回答案,则 API 会立即返回并显示错误并且不执行任何其他操作.所以必须有一些相关的方式来查询 API 是否准备好被调用(即以一种有效的方式模拟等待,避免在紧密循环中手动轮询).
  • 异步意味着 API 总是立即返回,已经启动了后台"工作来满足您的请求,因此必须有某种相关的方式来获取结果.

这篇关于异步和非阻塞调用?也在阻塞和同步之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆