eager.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  3. <title>eager&lt;T&gt;/atomic_eager&lt;T&gt; - Boost.Outcome documentation</title>
  4. <link rel="stylesheet" href="../../css/boost.css" type="text/css">
  5. <meta name="generator" content="Hugo 0.52 with Boostdoc theme">
  6. <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
  7. <link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
  8. <body><div class="spirit-nav">
  9. <a accesskey="p" href="../../reference/aliases/default_policy.html"><img src="../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../reference/aliases.html"><img src="../../images/up.png" alt="Up"></a>
  11. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../reference/aliases/lazy.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both"><code>eager&lt;T&gt;/atomic_eager&lt;T&gt;</code></h1></div></div></div>
  13. <p>This is very similar to <a href="../../reference/aliases/lazy.html" class="api-reference"><code>lazy&lt;T&gt;</code></a>
  14. , except that execution of the <code>eager&lt;T&gt;</code>
  15. returning function begins immediately, and if the function never suspends during the
  16. course of its execution, no suspend-resume cycle occurs. Functions which return <code>eager&lt;T&gt;</code>
  17. are therefore suitable for tasks which <em>may</em> require suspension, but will often complete
  18. immediately.</p>
  19. <p><code>atomic_eager&lt;T&gt;</code> is like <code>eager&lt;T&gt;</code>, except that the setting of the coroutine result
  20. performs an atomic release, whilst the checking of whether the coroutine has finished
  21. is an atomic acquire.</p>
  22. <p>Example of use (must be called from within a coroutinised function):</p>
  23. <div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">eager</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">func</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span>
  24. <span class="p">{</span>
  25. <span class="n">co_return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
  26. <span class="p">}</span>
  27. <span class="p">...</span>
  28. <span class="c1">// Executes like a non-coroutine function i.e. r is immediately set to 6.
  29. </span><span class="c1"></span><span class="kt">int</span> <span class="n">r</span> <span class="o">=</span> <span class="n">co_await</span> <span class="n">func</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span>
  30. </code></pre></div>
  31. <p><code>eager&lt;T&gt;</code> has special semantics if <code>T</code> is a type capable of constructing from
  32. an <code>exception_ptr</code> or <code>error_code</code> &ndash; any exceptions thrown during the function&rsquo;s body
  33. are sent via <code>T</code>, preferably via the error code route if <a href="../../reference/functions/error_from_exception.html" class="api-reference"><code>error_from_exception(</code></a>
  34. <code>)</code>
  35. successfully matches the exception throw. This means that a <a href="../../reference/types/basic_result.html" class="api-reference"><code>basic_result&lt;T, E, NoValuePolicy&gt;</code></a>
  36. or <a href="../../reference/types/basic_outcome.html" class="api-reference"><code>basic_outcome&lt;T, EC, EP, NoValuePolicy&gt;</code></a>
  37. where one of its types is
  38. is compatible will have its <code>.error()</code> or <code>.exception()</code> set.</p>
  39. <p>Note that <code>eager&lt;T&gt;</code> does not otherwise transport exception throws, and rethrows
  40. any exceptions thrown within the coroutine body through the coroutine machinery.
  41. This does not produce reliable consequences in current C++ compilers. You should
  42. therefore wrap the coroutine body in a <code>try...catch</code> if <code>T</code> is not able to transport
  43. exceptions on its own.</p>
  44. <p><em>Requires</em>: C++ coroutines to be available in your compiler.</p>
  45. <p><em>Namespace</em>: <code>BOOST_OUTCOME_V2_NAMESPACE::awaitables</code></p>
  46. <p><em>Header</em>: <code>&lt;boost/outcome/coroutine_support.hpp&gt;</code></p>
  47. </div><p><small>Last revised: October 04, 2019 at 15:58:37 &#43;0100</small></p>
  48. <hr>
  49. <div class="spirit-nav">
  50. <a accesskey="p" href="../../reference/aliases/default_policy.html"><img src="../../images/prev.png" alt="Prev"></a>
  51. <a accesskey="u" href="../../reference/aliases.html"><img src="../../images/up.png" alt="Up"></a>
  52. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../reference/aliases/lazy.html"><img src="../../images/next.png" alt="Next"></a></div></body>
  53. </html>