Note that there are some explanatory texts on larger screens.

plurals
  1. POimplementing future::then() equivalent for asynchronous execution in c++11
    primarykey
    data
    text
    <p>I have a few questions about the implementation of the function <code>then()</code> in <a href="http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Herb-Sutter-Concurrency-and-Parallelism" rel="nofollow noreferrer">Herb Sutter's talk</a>. This function is used to chain asynchronous operations, the parameter <code>f</code> is a future from one operation and the parameter <code>w</code> is the 'work' for this operation (lambda).</p> <pre><code>template &lt;typename Fut, typename Work&gt; auto then(Fut f, Work w) -&gt; future&lt;decltype(w(f.get()))&gt; { return async([=]{ w(f.get()); }); } </code></pre> <p>An example of application would be:</p> <pre><code> std::future&lt;int&gt; f = std::async([]{ std::this_thread::sleep_for(std::chrono::microseconds(200)); return 10; }); auto f2 = then(std::move(f), [](int i){ return 2 * i; }); </code></pre> <p>The main thread spawns the tasks but does not wait for any of them to finish.</p> <p>Firstly, <code>future&lt;T&gt;</code> does not have a copy constructor. This means, that the suggested implementation can be only used with <code>shared_future&lt;T&gt;</code> unless we change the call to <code>async()</code> to move the future into the lambda. <a href="https://stackoverflow.com/questions/14200678/c11-async-continuations-or-attempt-at-then-semantics/14200861#comment20089049_14200861">This SO question</a> suggested a way of doing it but it seems too complicated. I re-implemented the function and I am wondering whether my code is correct or whether I missed something...</p> <p>Secondly, the future that is passed to the <code>then()</code> function might be <code>void</code> so we actually need 2 implementations of <code>then()</code>, right? One for futures returning <code>T</code> and one for futures returning <code>void</code>.</p> <p>Lastly, should the lambda inside the body of <code>then()</code> not have a return statement so that we can actually return the value back? Without the return statement, then returns <code>future&lt;void&gt;</code>, right?</p> <p>I tried to address the above points and this is what I came up with. Is it correct?</p> <pre><code>template &lt;typename T, typename Work&gt; auto then(future&lt;T&gt; f, Work w) -&gt; future&lt;decltype(w(f.get()))&gt; { return async([](future&lt;T&gt; f, Work w) { return w(f.get()); }, move(f), move(w)); } template &lt;typename Work&gt; auto then(future&lt;void&gt; f, Work w) -&gt; future&lt;decltype(w())&gt; { return async([](future&lt;void&gt; f, Work w) { f.wait(); return w(); }, move(f), move(w)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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