Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In short, <code>boost::bind</code> creates a copy of the <code>boost::shared_ptr&lt;Connection&gt;</code> that is returned from <code>shared_from_this()</code>, and <code>boost::asio</code> may create a copy of the handler. The copy of the handler will remain alive until one of the following occurs:</p> <ul> <li>The handler has been called by a thread from which the service's <code>run()</code>, <code>run_one()</code>, <code>poll()</code> or <code>poll_one()</code> member function has been invoked.</li> <li>The <code>io_service</code> is destroyed.</li> <li>The <code>io_service::service</code> that owns the handler is shutdown via <a href="http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/reference/io_service__service/shutdown_service.html"><code>shutdown_service()</code></a>.</li> </ul> <p>Here are the relevant excerpts from the documentation:</p> <ul> <li><p>boost::bind <a href="http://www.boost.org/doc/libs/1_50_0/libs/bind/bind.html#with_functions">documentation</a>:</p> <blockquote> <p>The arguments that <code>bind</code> takes are copied and held internally by the returned function object.</p> </blockquote></li> <li><p>boost::asio <a href="http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/reference/io_service/post.html"><code>io_service::post</code></a>:</p> <blockquote> <p>The <code>io_service</code> guarantees that the handler will only be called in a thread in which the <code>run()</code>, <code>run_one()</code>, <code>poll()</code> or <code>poll_one()</code> member functions is currently being invoked. [...] The <code>io_service</code> will make a copy of the <em>handler</em> object as required.</p> </blockquote></li> <li><p>boost::asio <a href="http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/reference/io_service/_io_service.html"><code>io_service::~io_service</code></a>:</p> <blockquote> <p>Uninvoked handler objects that were scheduled for deferred invocation on the <code>io_service</code>, or any associated strand, are destroyed. <br/><br/> Where an object's lifetime is tied to the lifetime of a connection (or some other sequence of asynchronous operations), a <code>shared_ptr</code> to the object would be bound into the handlers for all asynchronous operations associated with it. [...] When a single connection ends, all associated asynchronous operations complete. The corresponding handler objects are destroyed, and all <code>shared_ptr</code> references to the objects are destroyed.</p> </blockquote></li> </ul> <hr> <p>While dated (2007), the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2175.pdf">Networking Library Proposal for TR2 (Revision 1)</a> was derived from Boost.Asio. Section <code>5.3.2.7. Requirements on asynchronous operations</code> provides some details for the arguments to <code>async_</code> functions:</p> <blockquote> <p>In this clause, an asynchronous operation is initiated by a function that is named with the prefix <code>async_</code>. These functions shall be known as <em>initiating functions</em>. [...] The library implementation may make copies of the handler argument, and the original handler argument and all copies are interchangeable.</p> <p>The lifetime of arguments to initiating functions shall be treated as follows:</p> <ul> <li>If the parameter is declared as a const reference or by-value [...] the implementation may make copies of the argument, and all copies shall be destroyed no later than immediately after invocation of the handler. </li> </ul> <p>[...] Any calls made by the library implementation to functions associated with the initiating function's arguments will be performed such that calls occur in a sequence call<sub><em>1</em></sub> to call<sub><em>n</em></sub>, where for all <em>i</em>, 1 ≤ <em>i</em> &lt; <em>n</em>, call<sub><em>i</em></sub> precedes call <sub><em>i+1</em></sub>.</p> </blockquote> <p>Thus:</p> <ul> <li>The implementation may create a copy of the <em>handler</em>. In the example, the copied <em>handler</em> will create a copy of the <code>shared_ptr&lt;Connection&gt;</code>, increasing the reference count of the <code>Connection</code> instance while the copies of <em>handler</em> remain alive.</li> <li>The implementation may destroy the <em>handler</em> prior to invoking <em>handler</em>. This occurs if the async operation is outstanding when <code>io_serive::service</code> is shutdown or the <code>io_service</code> is destroyed. In the example, the copies of handler will be destroyed, decreasing the reference count of <code>Connection</code>, and potentially causing the <code>Connection</code> instance to be destroyed.</li> <li>If <em>handler</em> is invoked, then all copies of handler will immediately be destroyed once execution returns from the handler. Again, the copies of handler will be destroyed, decreasing the reference count of <code>Connection</code>, and potentially causing it to be destroyed.</li> <li>The functions associated with the <code>asnyc_</code>'s arguments, will be executed sequentially, and not concurrent. This includes <code>io_handler_deallocate</code> and <code>io_handler_invoke</code>. This guarantees that the <em>handler</em> will not be deallocated while the <em>handler</em> is being invoked. In most areas of the <code>boost::asio</code> implementation, the <em>handler</em> is copied or moved to stack variables, allowing the destruction to occur once execution exits the block in which it was declared. In the example, this ensures that the reference count for <code>Connection</code> will be at least one during the invocation of the <em>handler</em>.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload