boost asio note 1 – asynchronous operation

Table of Contents

file

An asynchronous operation is the basic unit of composition in the Boost.Asio asynchronous model. Asynchronous operations represent work that is launched and performed in the background, while the user's code that initiated the work can continue with other things.

Conceptually, the lifecycle of an asynchronous operation can be described in terms of the following events and phases:

异步操作是 Boost.Asio 异步模型中的基本组合单元。 异步操作表示在后台启动和执行的工作,而启动该工作的用户代码可以继续执行其他操作。

从概念上讲,异步操作的生命周期可以用以下事件和阶段来描述:

file

An initiating function is a function which may be called by the user to start an asynchronous operation.

A completion handler is a user-provided, move-only function object that will be invoked, at most once, with the result of the asynchronous operation. The invocation of the completion handler tells the user about something that has already happened: the operation completed, and the side effects of the operation were established.

The initiating function and completion handler are incorporated into the user's code as follows:

启动函数是可以由用户调用以启动异步操作的函数。

完成处理程序是一个用户提供的、只能移动的函数对象,它最多被调用一次,并带有异步操作的结果。 完成处理程序的调用告诉用户已经发生的事情:操作完成,并且操作的效果已经产生。

启动函数和完成处理程序合并到用户代码中,如下所示:

file

Synchronous operations, being embodied as single functions, have several inherent semantic properties as a consequence. Asynchronous operations adopt some of these semantic properties from their synchronous counterparts, in order to facilitate flexible and efficient composition.

作为单一功能体现的同步操作因此具有几个固有的语义属性。 异步操作从同步操作中采用其中一些语义属性,以促进灵活高效的组合。

同步操作

When a synchronous operation is generic (i.e. a template) the return type is deterministically derived from the function and its arguments.

If a synchronous operation requires a temporary resource (such as memory, a file descriptor, or a thread), this resource is released before returning from the function.

1) 当同步操作是通用的(例如模板)时,返回类型是确定性地派生于函数及其参数。

2) 如果同步操作需要临时资源(如内存、文件描述符或线程),则在从函数返回之前释放该资源。

异步操作

When an asynchronous operation is generic, the completion handler's arguments' types and order are deterministically derived from the initiating function and its arguments.

If an asynchronous operation requires a temporary resource (such as memory, a file descriptor, or a thread), this resource is released before calling the completion handler.

1) 当异步操作是通用的时,完成处理程序的参数类型和顺序是确定性地从启动函数及其参数派生的。

2) 如果异步操作需要临时资源(如内存、文件描述符或线程),则在调用完成处理程序之前释放此资源。

The latter is an important property of asynchronous operations, in that it allows a completion handler to initiate further asynchronous operations without overlapping resource usage. Consider the trivial (and relatively common) case of the same operation being repeated over and over in a chain:

后者是异步操作的一个重要属性,因为它允许完成处理程序在不重叠资源使用的情况下启动进一步的异步操作。 考虑在链中反复重复相同操作的微不足道(且相对常见)的情况:

file

By ensuring that resources are released before the completion handler runs, we avoid doubling the peak resource usage of the chain of operations.

通过确保在完成处理程序运行之前释放资源,我们避免了链式操作时对于峰值资源使用量的加倍使用。

Comments |0|

Legend *) Required fields are marked
**) You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Category: boost