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>lazy<T>/atomic_lazy<T> - 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 8<link rel="icon" href="../../images/favicon.ico" type="image/ico"/> 9<body><div class="spirit-nav"> 10<a accesskey="p" href="../../reference/aliases/eager.html"><img src="../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../reference/aliases.html"><img src="../../images/up.png" alt="Up"></a> 12 <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../reference/aliases/outcome.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content"> 13 <div class="titlepage"><div><div><h1 style="clear: both"><code>lazy<T>/atomic_lazy<T></code></h1></div></div></div> 14 <p>This is very similar to <a href="../../reference/aliases/eager.html" class="api-reference"><code>eager<T></code></a> 15, except that execution of the 16<code>lazy<T></code> returning function suspends immediately. Functions which return <code>lazy<T></code> 17are therefore suitable for tasks which you need to instantiate right now, but whose 18execution will occur elsewhere e.g. in a separate kernel thread. Because of the very 19common use case of using worker threads to execute the body of lazily executed 20coroutines, most people will want to use <code>atomic_lazy<T></code> instead of <code>lazy<T></code>.</p> 21 22<p><code>atomic_lazy<T></code> is like <code>lazy<T></code>, except that the setting of the coroutine result 23performs an atomic release, whilst the checking of whether the coroutine has finished 24is an atomic acquire.</p> 25 26<p><code>lazy<T></code> has similar semantics to <code>std::lazy<T></code>, which is being standardised. See 27<a href="https://wg21.link/P1056">https://wg21.link/P1056</a> <em>Add lazy coroutine (coroutine task) type</em>.</p> 28 29<p>Example of use (must be called from within a coroutinised function):</p> 30<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">lazy</span><span class="o"><</span><span class="kt">int</span><span class="o">></span> <span class="n">func</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> 31<span class="p">{</span> 32 <span class="n">co_return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> 33<span class="p">}</span> 34<span class="p">...</span> 35<span class="c1">// Always suspends perhaps causing other coroutines to execute, then resumes. 36</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> 37</code></pre></div> 38<p><code>lazy<T></code> has special semantics if <code>T</code> is a type capable of constructing from 39an <code>exception_ptr</code> or <code>error_code</code> – any exceptions thrown during the function’s body 40are 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> 41<code>)</code> 42successfully matches the exception throw. This means that a <a href="../../reference/types/basic_result.html" class="api-reference"><code>basic_result<T, E, NoValuePolicy></code></a> 43 44or <a href="../../reference/types/basic_outcome.html" class="api-reference"><code>basic_outcome<T, EC, EP, NoValuePolicy></code></a> 45 where one of its types is 46is compatible will have its <code>.error()</code> or <code>.exception()</code> set.</p> 47 48<p>Note that <code>lazy<T></code> does not otherwise transport exception throws, and rethrows 49any exceptions thrown within the coroutine body through the coroutine machinery. 50This does not produce reliable consequences in current C++ compilers. You should 51therefore wrap the coroutine body in a <code>try...catch</code> if <code>T</code> is not able to transport 52exceptions on its own.</p> 53 54<p><em>Requires</em>: C++ coroutines to be available in your compiler.</p> 55 56<p><em>Namespace</em>: <code>BOOST_OUTCOME_V2_NAMESPACE::awaitables</code></p> 57 58<p><em>Header</em>: <code><boost/outcome/coroutine_support.hpp></code></p> 59 60 61 </div><p><small>Last revised: October 04, 2019 at 15:58:37 +0100</small></p> 62<hr> 63<div class="spirit-nav"> 64<a accesskey="p" href="../../reference/aliases/eager.html"><img src="../../images/prev.png" alt="Prev"></a> 65 <a accesskey="u" href="../../reference/aliases.html"><img src="../../images/up.png" alt="Up"></a> 66 <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../reference/aliases/outcome.html"><img src="../../images/next.png" alt="Next"></a></div></body> 67</html> 68