大约需要同步和异步操作ASIO澄清 [英] Some clarification needed about synchronous versus asynchronous asio operations

查看:364
本文介绍了大约需要同步和异步操作ASIO澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,同步和异步操作之间的主要区别。即的write()阅读() VS async_write() async_read()是前者,不返回,直到操作完成-or无差错,以及最后的,立即返回。

As far as I know, the main difference between synchronous and asynchronous operations. I.e. write() or read() vs async_write() and async_read() is that the former, don't return until the operation finish -or error-, and the last ones, returns immediately.

由于事实,即异步操作是由一个 io_service.run控制()不完成,直到控制操作已完成。在我看来,在顺序操作中所涉及的与协议例如POP3 TCP / IP连接,其中,所述操作是一个序列,例如:

Due the fact that the asynchronous operations are controlled by an io_service.run() that does not finish until the controlled operations has finalized. It seems to me that in sequential operations as those involved in TCP/IP connections with protocols such as POP3, in which the operation is a sequence such as:

 C: <connect>
 S: Ok.
 C: User...
 S: Ok.
 C: Password
 S: Ok.
 C: Command
 S: answer
 C: Command
 S: answer
 ...
 C: bye
 S: <close>

同步/异步运营商之间的差异并不太大的意义。

The difference between synchronous/asynchronous operators does not make much sense.

当然,在这两个操作总是有程序流程无限期地停止某些情况-there使用timers-的风险,但我想知道在这个问题上的一些更多的授权意见。

Of course, in both operations there is always the risk that the program flow stops indefinitely by some circumstance -there the use of timers-, but I would like know some more authorized opinions in this matter.

我必须承认,这个问题是相当不明确的,但我想听到有关何时使用一个或其他一些建议。我已经与微软的Visual Studio中调试在POP3客户端上,我现在的工作对于异步SSL操作时遇到问题,有时想,也许这是一个坏主意采用异步在这。

I must admit that the question is rather ill-defined, but I would like to hear some advice about when to use one or the other. I've run into problems when debugging with MS Visual Studio regarding asynchronous SSL operations in a POP3 client on which I'm now working, and sometimes think that perhaps it is a bad idea use asynchronous in this.

推荐答案

Boost.Asio的文档确实一个梦幻般的工作,解释两个概念。作为<一个href=\"http://stackoverflow.com/questions/5282659/some-clarification-needed-about-synchronous-versus-asynchronous-asio-operations/5283615#5283615\">Ralf提到克里斯也有描述异步概念的伟大的博客。该泊车咪表例子,说明如何超时工作是特别有趣,因为是< A HREF =htt​​p://blog.think-async.com/2010/04/bind-illustrated.html>绑定说明例子。

The Boost.Asio documentation really does a fantastic job explaining the two concepts. As Ralf mentioned, Chris also has a great blog describing asynchronous concepts. The parking meter example explaining how timeouts work is particularly interesting, as is the bind illustrated example.

首先,考虑同步连接操作:

First, consider a synchronous connect operation:

控制流是非常简单的在这里,你的程序调用API的一些(1)连接的插座。所述API使用一个I / O服务(2)中的操作系统(3)执行的操作。一旦该操作完成(4和5),控制返回到程序之后立即(6)成功或失败的指示。

The control flow is fairly straightforward here, your program invokes some API (1) to connect a socket. The API uses an I/O service (2) to perform the operation in the operating system (3). Once this operation is complete (4 and 5), control returns to your program immediately afterwards (6) with some indication of success or failure.

中类似的异步操作有着完全不同的控制流:

The analogous asynchronous operation has a completely different control flow:

下面,应用程序启动使用相同的I / O服务(2)的操作(1),但该控制流被反转。完成上述操作会导致I / O服务,通过完成处理程序来通知你的程序。步骤3,当操作完成了连接操作中完全包含了同步的情况下。

Here, your application initiates the operation (1) using the same I/O service (2), but the control flow is inverted. Completion of the operation causes the I/O service to notify your program through a completion handler. The time between step 3 and when the operation has completed was contained entirely within the connect operation for the synchronous case.

您可以看到同步的情况下,自然更容易为大多数程序员掌握,因为它重新presents传统的控制流的范例。通过异步操作使用反相控制流程是很难理解,常常迫使你的程序拆分业务并入启动处理方法,其中,所述逻辑被左右移动。但是,一旦你有这种控制的一个基本的理解流,你就会明白这个概念确实是多么强大。一些异步编程的优点是:

You can see the synchronous case is naturally easier for most programmers to grasp because it represents the traditional control flow paradigms. The inverted control flow used by asynchronous operations is difficult to understand, it often forces your program to split up operations into start and handle methods where the logic is shifted around. However, once you have a basic understanding of this control flow you'll realize how powerful the concept really is. Some of the advantages of asynchronous programming are:


  • 解耦并发线程。以一个长期运行的操作,同步情况下,你经常会创建一个单独的线程从不响应处理操作prevent应用程序的GUI。这个概念在小范围内正常工作,但几个thread,迅速分崩离析。

  • Decouples threading from concurrency. Take a long running operation, for the synchronous case you would often create a separate thread to handle the operation to prevent an application's GUI from becoming unresponsive. This concept works fine at a small scale, but quickly falls apart at a handful of threads.

来提高性能。线程每个连接的设计根本没有规模。请参阅 C10K问题

Increased Performance. The thread-per-connection design simply does not scale. See the C10K problem.

成分(或链接)。更高级别的操作可以由多个完成处理的。考虑转印JPEG图像,该协议可能规定第一40字节包括描述图象的大小,形状,也许有些其他信息的报头。第一完成处理程序,以发送该首标可以启动第二操作,以发送图像数据。更高级别的操作 sendImage()并不需要知道,也不关心,对方法链用于实现数据传输。

Composition (or Chaining). Higher level operations can be composed of multiple completion handlers. Consider transferring a JPEG image, the protocol might dictate the first 40 bytes include a header describing the image size, shape, maybe some other information. The first completion handler to send this header can initiate the second operation to send the image data. The higher level operation sendImage() does not need to know, or care, about the method chaining used to implement the data transfer.

超时并取消能力。有平台的具体方式超时一个长期运行的操作(例如: SO_RCVTIMEO SO_SNDTIMEO )。使用异步操作实现的使用 deadline_timer 取消所有支持的平台上长时间运行的操作。

Timeouts and cancel-ability. There are platform specific ways to timeout a long running operation (ex: SO_RCVTIMEO and SO_SNDTIMEO). Using asynchronous operations enables the usage of deadline_timer canceling long running operations on all supported platforms.

当然,在这两个操作有
  八方的风险程序流
  一些无限期停止
  circunstance -there使用
  timers-,但我想知道一些
  这更多的授权意见
  此事。

Of course, in both operations there is allways the risk that the program flow stops indefinitely by some circunstance -there the use of timers-, but I would like know some more authorized opinions in this matter.

我使用短耳亲身经历从可扩展性方面造成的。写作超​​级计算机软​​件有限资源,如内存,线程,插座等。使用时务需要照顾的相当数量的线程每个连接的〜2万并发操作是一个设计,是时已死亡。

My personal experience using Asio stems from the scalability aspect. Writing software for supercomputers requires a fair amount of care when dealing with limited resources such as memory, threads, sockets, etc. Using a thread-per-connection for ~2 million simultaneous operations is a design that is dead on arrival.

这篇关于大约需要同步和异步操作ASIO澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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