Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a few issues:</p> <ul> <li>First, there's no need to use <code>wait()</code> since that's implied by <code>get()</code>. </li> <li>Either <code>list</code> or <code>vector</code> works fine with <code>push_back</code>. You give the wrong number of arguments to <code>list::insert</code>. The best is to use in-place construction with <code>emplace_back</code></li> <li>It also seems you should only do <code>.get()</code> once. Subsequent calls to <code>get()</code> yields <code>std::future_error</code> exception.</li> <li>The syntax you used for constructing the futures does not exist. The simplest way to do things like that is with a lambda as below.</li> </ul> <h2>Full example:</h2> <pre><code>// g++ -pthread -std=c++0x #include &lt;iostream&gt; #include &lt;future&gt; #include &lt;list&gt; struct X { X(int v) : mv(v) {} int mv; int max_value() const { return mv; } }; int main(){ std::list&lt;std::future&lt;int&gt; &gt; results; X x4(4); X x5(5); X x3(3); results.emplace_back(std::async(std::launch::async, [&amp;x4](){ return x4.max_value();})); results.emplace_back(std::async(std::launch::async, [&amp;x5](){ return x5.max_value();})); results.emplace_back(std::async(std::launch::async, [&amp;x3](){ return x3.max_value();})); // for sure there's better ways to do this step, but for clarity: int best_value=0; for (auto &amp;r : results){ auto this_value=r.get(); if (best_value &lt; this_value) best_value = this_value; } std:: cout &lt;&lt; best_value &lt;&lt; std::endl; } </code></pre> <p>Since you use a shared pointer you could also make the lambda take the object by value,</p> <pre><code>std::shared_ptr&lt;SomeT&gt; some_obj= ... from somewhere... ; results.emplace_back( std::async( std::launch::async, [some_obj](){ return some_obs-&gt;max_value();} ) ); </code></pre>
 

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