异步调用是否需要当前进程中的额外线程或使用线程池中的另一个线程?

发布于 2024-09-24 00:40:36 字数 233 浏览 7 评论 0原文

我指的是这个答案,它说这不是必需的,但几乎没有具体的假设。这个问题很普遍。

  • 我使用 C#
  • 异步进程什么也不做,只是调用外部 API 并等待回复。

I'm referring to this answer where it says it's not required, there are few specific assumptions though. That question is general.

  • I'm using C#
  • Asynchronous process is doing nothing but just calling an external API and waiting for the reply.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情别恋 2024-10-01 00:40:36

一般来说是的,但我能想到至少有一个例外。要启动异步操作,您必须将该操作传输到调用者上下文之外。我的意思是调用者不应该阻塞等待操作完成。这通常意味着操作必须移动到新创建的线程、线程池中的线程、IO 完成端口、另一个进程等。

我说我想到了一个例外。如果我们稍微歪曲异步的定义,我们可以允许发起者不会阻塞等待操作完成而无需实际将操作移动到另一个线程的情况。最好的例子就是 UI 消息泵。在 .NET 中,从 UI 线程本身调用 Control.BeginInvoke 来在同一线程上发布委托的执行是很容易的。发起者显然不会阻塞等待委托完成,但委托最终将开始在同一线程上执行。这绝对是对我们通常认为的异步术语的歪曲,因为在这种情况下,操作会阻塞,直到调用者完成,而不是相反。

Generally yes, but there is at least one exception I can think of. To initiate an asynchronous operation you must transfer the operation outside of the context of the caller. What I mean by that is that the caller should not block waiting for the operation to complete. That usually means that the operation has to move to a newly created thread, a thread from the ThreadPool, an IO completion port, another process, or the like.

I said there was one exception that came to mind. If we slightly pervert our definition of asynchronous we can allow for scenarios in which the initiator does not block waiting for the operation to complete without actually moving the operation to another thread. The best example of this is the UI message pump. In .NET it is easy enough to call Control.BeginInvoke from the UI thread itself to post the execution of a delegate on the same thread. The initiator clearly would not block waiting for the delegate to complete and yet the delegate would eventually start executing on the same thread. This is definitely a perversion of what we typically think of the term asychronous because in this scenario the operation blocks until the caller completes instead of the other way around.

偏爱自由 2024-10-01 00:40:36

你的问题太笼统了。 Windows API 对不使用线程的异步 I/O 有特定支持,例如查看 ReadFile() 的文档。 lpOverlapped 参数控制这一点。 ReadFileEx() 函数允许指定 I/O 完成回调。 .NET 框架类很好地涵盖了这一点。

此机制不包括调用慢速外部 API 函数,您需要启动自己的线程。

Your question is too general. The Windows API has specific support for asynchronous I/O without using a thread, check the docs for ReadFile() for example. The lpOverlapped argument controls this. The ReadFileEx() function allows specifying an I/O completion callback. This is well covered by the .NET framework classes.

Calling a slow external API function is not covered by this mechanism, you need to spin up your own thread.

画骨成沙 2024-10-01 00:40:36

是的,使用了池线程。但该线程并没有等待外部设备。当 I/O 完成时,该线程将被重新使用,并调用一个(另一个)线程。

请参阅 MSDN 上的 I/O 异步完成

Yes, a Pool Thread is used. But that thread is not hanging around waiting for the external device. The thread is re-used and a(nother) thread is called upon when the I/O is completed.

See I/O Asynchronous Completion on MSDN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文