<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht?]]></title><description><![CDATA[<p>Konkretes Beispiel:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;future&gt;
#include &lt;thread&gt;
#include &lt;chrono&gt;
#include &lt;string&gt;

using namespace std;

////////////////////////////////////////////////////////////////////////////////////
// async should be the first choice. Threads are started and managed during runtime.

auto sleep = [](int s) { this_thread::sleep_for(chrono::seconds(s)); };

int provideTheNumberForEverything(int sec)
{
        sleep (sec);
        return 42;
}

int main()
{
    auto start_time = chrono::high_resolution_clock::now();

    ////////////////////////////////////////////////////////////////////////////////
    // future from an async()
    ////////////////////////////////////////////////////////////////////////////////

    auto result1 = async( /*launch::async,*/ &amp;provideTheNumberForEverything, 3);
    // future&lt;int&gt; result1 = async(/*launch::async*/, &amp;provideTheNumberForEverything,3);
    // future&lt;int&gt; result1 = async(/*launch::async*/, [](){ sleep(3); return 42; });

    ////////////////////////////////////////////////////////////////////////////////
    // future from a packaged_package
    ////////////////////////////////////////////////////////////////////////////////

    packaged_task&lt;string()&gt; package([](){ sleep(5); return &quot;The number&quot;; }); 
    future&lt;string&gt; result2 = package.get_future();  
    thread(move(package)).detach(); 

    ////////////////////////////////////////////////////////////////////////////////
    // future from a promise
    ////////////////////////////////////////////////////////////////////////////////

    promise&lt;string&gt; p;
    future&lt;string&gt; result3 = p.get_future();
    thread(  [](promise&lt;string&gt; p)
             { 
               sleep(7); p.set_value_at_thread_exit(&quot;is the answer to everything.&quot;); 
             }, 
             move(p)
           ).detach();

    //////////////////////////////// output ////////////////////////////////////////

    cout &lt;&lt; &quot;Waiting... &quot; &lt;&lt; endl;
    result1.wait();
    auto end_time = chrono::high_resolution_clock::now();
    cout &lt;&lt; &quot;result 1 ... time: &quot; &lt;&lt; chrono::duration_cast&lt;chrono::milliseconds&gt;(end_time - start_time).count() &lt;&lt; &quot; ms&quot; &lt;&lt; endl;
    result2.wait();
    end_time = chrono::high_resolution_clock::now();
    cout &lt;&lt; &quot;result 2 ... time: &quot; &lt;&lt; chrono::duration_cast&lt;chrono::milliseconds&gt;(end_time - start_time).count() &lt;&lt; &quot; ms&quot; &lt;&lt; endl;
    result3.wait();
    end_time = chrono::high_resolution_clock::now();
    cout &lt;&lt; &quot;result 3 ... time: &quot; &lt;&lt; chrono::duration_cast&lt;chrono::milliseconds&gt;(end_time - start_time).count() &lt;&lt; &quot; ms&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;\nDone!\nResults are: &quot; &lt;&lt; result2.get() &lt;&lt; ' ' &lt;&lt; result1.get() &lt;&lt; ' ' &lt;&lt; result3.get() &lt;&lt; '\n';
}
</code></pre>
<p>Mit std::launch::async kann ich einen Extra-Thread erzwingen, mit std::launch::deferred einen unterdrücken. Wie ist das, wenn ich nichts angebe, es also C++ zur laufzeit überlasse? Wie erkenne ich, ob std::async zur Laufzeit einen extra Thread erzeugt oder nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/334162/wie-erkenne-ich-ob-std-async-einen-extra-thread-erzeugt-oder-nicht</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 02:37:30 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/334162.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 25 Aug 2015 15:02:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 15:02:07 GMT]]></title><description><![CDATA[<p>Konkretes Beispiel:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;future&gt;
#include &lt;thread&gt;
#include &lt;chrono&gt;
#include &lt;string&gt;

using namespace std;

////////////////////////////////////////////////////////////////////////////////////
// async should be the first choice. Threads are started and managed during runtime.

auto sleep = [](int s) { this_thread::sleep_for(chrono::seconds(s)); };

int provideTheNumberForEverything(int sec)
{
        sleep (sec);
        return 42;
}

int main()
{
    auto start_time = chrono::high_resolution_clock::now();

    ////////////////////////////////////////////////////////////////////////////////
    // future from an async()
    ////////////////////////////////////////////////////////////////////////////////

    auto result1 = async( /*launch::async,*/ &amp;provideTheNumberForEverything, 3);
    // future&lt;int&gt; result1 = async(/*launch::async*/, &amp;provideTheNumberForEverything,3);
    // future&lt;int&gt; result1 = async(/*launch::async*/, [](){ sleep(3); return 42; });

    ////////////////////////////////////////////////////////////////////////////////
    // future from a packaged_package
    ////////////////////////////////////////////////////////////////////////////////

    packaged_task&lt;string()&gt; package([](){ sleep(5); return &quot;The number&quot;; }); 
    future&lt;string&gt; result2 = package.get_future();  
    thread(move(package)).detach(); 

    ////////////////////////////////////////////////////////////////////////////////
    // future from a promise
    ////////////////////////////////////////////////////////////////////////////////

    promise&lt;string&gt; p;
    future&lt;string&gt; result3 = p.get_future();
    thread(  [](promise&lt;string&gt; p)
             { 
               sleep(7); p.set_value_at_thread_exit(&quot;is the answer to everything.&quot;); 
             }, 
             move(p)
           ).detach();

    //////////////////////////////// output ////////////////////////////////////////

    cout &lt;&lt; &quot;Waiting... &quot; &lt;&lt; endl;
    result1.wait();
    auto end_time = chrono::high_resolution_clock::now();
    cout &lt;&lt; &quot;result 1 ... time: &quot; &lt;&lt; chrono::duration_cast&lt;chrono::milliseconds&gt;(end_time - start_time).count() &lt;&lt; &quot; ms&quot; &lt;&lt; endl;
    result2.wait();
    end_time = chrono::high_resolution_clock::now();
    cout &lt;&lt; &quot;result 2 ... time: &quot; &lt;&lt; chrono::duration_cast&lt;chrono::milliseconds&gt;(end_time - start_time).count() &lt;&lt; &quot; ms&quot; &lt;&lt; endl;
    result3.wait();
    end_time = chrono::high_resolution_clock::now();
    cout &lt;&lt; &quot;result 3 ... time: &quot; &lt;&lt; chrono::duration_cast&lt;chrono::milliseconds&gt;(end_time - start_time).count() &lt;&lt; &quot; ms&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;\nDone!\nResults are: &quot; &lt;&lt; result2.get() &lt;&lt; ' ' &lt;&lt; result1.get() &lt;&lt; ' ' &lt;&lt; result3.get() &lt;&lt; '\n';
}
</code></pre>
<p>Mit std::launch::async kann ich einen Extra-Thread erzwingen, mit std::launch::deferred einen unterdrücken. Wie ist das, wenn ich nichts angebe, es also C++ zur laufzeit überlasse? Wie erkenne ich, ob std::async zur Laufzeit einen extra Thread erzeugt oder nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2465543</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465543</guid><dc:creator><![CDATA[Erhard Henkes]]></dc:creator><pubDate>Tue, 25 Aug 2015 15:02:07 GMT</pubDate></item><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 15:11:00 GMT]]></title><description><![CDATA[<p>1. Warum willst Du das ueberhaupt wissen?</p>
<p>2. Async duerfte in den allermeisten Faellen die falsche Wahl sein. Schreib dir lieber nen Thread-Pool oder so.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2465544</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465544</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 25 Aug 2015 15:11:00 GMT</pubDate></item><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 15:25:46 GMT]]></title><description><![CDATA[<blockquote>
<p>Async duerfte in den allermeisten Faellen die falsche Wahl sein.</p>
</blockquote>
<p>async ist erste Wahl (R. Grimm, C++11 für Programmierer, Praxistipp)</p>
<blockquote>
<p>Warum willst Du das ueberhaupt wissen?</p>
</blockquote>
<p>Aus Interesse.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2465546</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465546</guid><dc:creator><![CDATA[Erhard Henkes]]></dc:creator><pubDate>Tue, 25 Aug 2015 15:25:46 GMT</pubDate></item><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 15:37:53 GMT]]></title><description><![CDATA[<p>Erhard Henkes schrieb:</p>
<blockquote>
<p>Mit std::launch::async kann ich einen Extra-Thread erzwingen, mit std::launch::deferred einen unterdrücken.</p>
</blockquote>
<p>Erzwingen und unterdrücken sind IMHO nicht ganz richtig.</p>
<p><a href="http://en.cppreference.com/w/cpp/thread/async" rel="nofollow">http://en.cppreference.com/w/cpp/thread/async</a> schrieb:</p>
<blockquote>
<p>Behaves the same as async(std::launch::async | std::launch::deferred, f, args...). In other words, f may be executed in another thread or it may be run synchronously when the resulting std::future is queried for a value.</p>
</blockquote>
<p>für async <strong>ohne</strong> Angaben.</p>
<p>Erhard Henkes schrieb:</p>
<blockquote>
<p>Wie ist das, wenn ich nichts angebe, es also C++ zur laufzeit überlasse? Wie erkenne ich, ob std::async zur Laufzeit einen extra Thread erzeugt oder nicht?</p>
</blockquote>
<p>- Debugger<br />
- Top<br />
- Threadids ausgeben lassen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2465554</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465554</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Tue, 25 Aug 2015 15:37:53 GMT</pubDate></item><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 15:43:55 GMT]]></title><description><![CDATA[<p>Erhard Henkes schrieb:</p>
<blockquote>
<p>async ist erste Wahl (R. Grimm, C++11 für Programmierer, Praxistipp)</p>
</blockquote>
<p>Und weil's in einem Buch steht, muss es richtig sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2465555</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465555</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 25 Aug 2015 15:43:55 GMT</pubDate></item><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 15:51:28 GMT]]></title><description><![CDATA[<p>cooky451 schrieb:</p>
<blockquote>
<p>2. Async duerfte in den allermeisten Faellen die falsche Wahl sein.</p>
</blockquote>
<p>Warum?</p>
<p>cooky451 schrieb:</p>
<blockquote>
<p>Schreib dir lieber nen Thread-Pool oder so.</p>
</blockquote>
<p>So ganz trivial dürfte das selber schreiben aber auch nicht sein. Ob das wirklich jeder machen sollte?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2465558</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465558</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Tue, 25 Aug 2015 15:51:28 GMT</pubDate></item><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 17:02:13 GMT]]></title><description><![CDATA[<p>Ich wiederhole die Frage, auf deren klare Beantwortung ich noch warte:</p>
<blockquote>
<p>Wie erkenne ich, ob std::async zur Laufzeit einen extra Thread erzeugt oder nicht?</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2465570</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465570</guid><dc:creator><![CDATA[Erhard Henkes]]></dc:creator><pubDate>Tue, 25 Aug 2015 17:02:13 GMT</pubDate></item><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 17:53:54 GMT]]></title><description><![CDATA[<p>Erhard Henkes schrieb:</p>
<blockquote>
<p>Ich wiederhole die Frage, auf deren klare Beantwortung ich noch warte:</p>
<blockquote>
<p>Wie erkenne ich, ob std::async zur Laufzeit einen extra Thread erzeugt oder nicht?</p>
</blockquote>
</blockquote>
<p>Lesen hilft</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2465574</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465574</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Tue, 25 Aug 2015 17:53:54 GMT</pubDate></item><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 19:31:10 GMT]]></title><description><![CDATA[<p>Ueberpruefen, ob gerade in einem anderen Thread laeuft, wird mit der thread id gehen. Von aussen geht das wohl kaum zuverlaessig.</p>
<p>std::async ist in der theorie ganz gut, weil die library auf weitere Informationen, im Prinzip sogar auf codelaengenabschaetzung, zurueckgreifen koennte. Praktisch sind die aber oft ganz primitiv implementiert mit &quot;starte es deferred, wenn nicht anders angegeben&quot; (gcc), sodass eine implementierung per threadpool oft deutlich schneller ist.</p>
<p>haskell und go zeigen, wie es sein koennte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2465590</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465590</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Tue, 25 Aug 2015 19:31:10 GMT</pubDate></item><item><title><![CDATA[Reply to Wie erkenne ich, ob std::async einen extra Thread erzeugt oder nicht? on Tue, 25 Aug 2015 19:53:16 GMT]]></title><description><![CDATA[<p>Yep.</p>
<blockquote>
<p>- Debugger<br />
- Top<br />
- Threadids ausgeben lassen</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2465593</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2465593</guid><dc:creator><![CDATA[Erhard Henkes]]></dc:creator><pubDate>Tue, 25 Aug 2015 19:53:16 GMT</pubDate></item></channel></rss>