<?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[Exception fliegt in Testprogramm zu std::async]]></title><description><![CDATA[<p>Hi,<br />
ich wollte ein kleines Testprogramm schreiben, das mir die ids der Threads anzeigt, die von std::async gespawnt werden (falls sie gespawnt werden).</p>
<p>Mein Programm sieht so aus:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;thread&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;
#include &lt;future&gt;
#include &lt;algorithm&gt;

// summiere alle Werte in der Range auf
int sum(const int * a, const int * b)
{
        std::cout &lt;&lt; std::this_thread::get_id() &lt;&lt; ' ';

        auto distance = b-a;
        if(distance &lt; 5) // willkuerliche Grenze
                return std::accumulate(a,b,0);

        const int * half = a + distance/2;

        auto fut_left = std::async(sum, a, half);
        auto right = sum(half, b);

        return fut_left.get() + right;
}

int main()
{
        std::vector&lt;int&gt; vec(1024);
        std::iota(vec.begin(), vec.end(), 0);
        std::cout &lt;&lt; sum(vec.data(), vec.data()+1024) &lt;&lt; '\n';
}
</code></pre>
<p>Mit g++ 4.8.1 fliegt eine Exception:</p>
<pre><code>terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread Abgebrochen
</code></pre>
<p>Hier im online Compiler: <a href="http://ideone.com/a8DApC" rel="nofollow">http://ideone.com/a8DApC</a></p>
<p>Sieht jemand, wo ich hier Mist baue?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/327635/exception-fliegt-in-testprogramm-zu-std-async</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Jul 2026 22:44:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/327635.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 24 Aug 2014 13:41:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Exception fliegt in Testprogramm zu std::async on Sun, 24 Aug 2014 13:41:42 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich wollte ein kleines Testprogramm schreiben, das mir die ids der Threads anzeigt, die von std::async gespawnt werden (falls sie gespawnt werden).</p>
<p>Mein Programm sieht so aus:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;thread&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;
#include &lt;future&gt;
#include &lt;algorithm&gt;

// summiere alle Werte in der Range auf
int sum(const int * a, const int * b)
{
        std::cout &lt;&lt; std::this_thread::get_id() &lt;&lt; ' ';

        auto distance = b-a;
        if(distance &lt; 5) // willkuerliche Grenze
                return std::accumulate(a,b,0);

        const int * half = a + distance/2;

        auto fut_left = std::async(sum, a, half);
        auto right = sum(half, b);

        return fut_left.get() + right;
}

int main()
{
        std::vector&lt;int&gt; vec(1024);
        std::iota(vec.begin(), vec.end(), 0);
        std::cout &lt;&lt; sum(vec.data(), vec.data()+1024) &lt;&lt; '\n';
}
</code></pre>
<p>Mit g++ 4.8.1 fliegt eine Exception:</p>
<pre><code>terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread thread::id of a non-executing thread Abgebrochen
</code></pre>
<p>Hier im online Compiler: <a href="http://ideone.com/a8DApC" rel="nofollow">http://ideone.com/a8DApC</a></p>
<p>Sieht jemand, wo ich hier Mist baue?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2414739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2414739</guid><dc:creator><![CDATA[asdfasd]]></dc:creator><pubDate>Sun, 24 Aug 2014 13:41:42 GMT</pubDate></item><item><title><![CDATA[Reply to Exception fliegt in Testprogramm zu std::async on Sun, 24 Aug 2014 14:17:49 GMT]]></title><description><![CDATA[<p>Hast du pthread gelinkt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2414742</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2414742</guid><dc:creator><![CDATA[pthread]]></dc:creator><pubDate>Sun, 24 Aug 2014 14:17:49 GMT</pubDate></item><item><title><![CDATA[Reply to Exception fliegt in Testprogramm zu std::async on Sun, 24 Aug 2014 14:34:10 GMT]]></title><description><![CDATA[<p>Du hast recht! Wenn ich pthread linke, dann funktioniert das Programm. Ist es eigentlich standardkonform, pthread linken zu müssen, damit die Standard-Threads funktionieren?</p>
<p>Und leider wird nichts parallel ausgeführt (alle Thread ids sind die gleichen), obwohl mein 4-Kern CPU eine Auslastung von 0% bei Start des Programms hat. Scheinbar traut gcc meinem System nichts zu <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2414743</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2414743</guid><dc:creator><![CDATA[asdfasd]]></dc:creator><pubDate>Sun, 24 Aug 2014 14:34:10 GMT</pubDate></item><item><title><![CDATA[Reply to Exception fliegt in Testprogramm zu std::async on Sun, 24 Aug 2014 16:28:20 GMT]]></title><description><![CDATA[<p>Ob und was du linken musst ist alles implementationsspezifisch, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2414750</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2414750</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sun, 24 Aug 2014 16:28:20 GMT</pubDate></item><item><title><![CDATA[Reply to Exception fliegt in Testprogramm zu std::async on Sun, 24 Aug 2014 17:37:01 GMT]]></title><description><![CDATA[<p>asdfasd schrieb:</p>
<blockquote>
<p>Und leider wird nichts parallel ausgeführt (alle Thread ids sind die gleichen), obwohl mein 4-Kern CPU eine Auslastung von 0% bei Start des Programms hat. Scheinbar traut gcc meinem System nichts zu <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
</blockquote>
<p>Hast du auch mit -pthread übersetzt (nicht nur gelinkt! Wobei -pthread auch ein Linken gegen pthread impliziert)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2414768</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2414768</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 24 Aug 2014 17:37:01 GMT</pubDate></item><item><title><![CDATA[Reply to Exception fliegt in Testprogramm zu std::async on Sun, 24 Aug 2014 19:36:42 GMT]]></title><description><![CDATA[<p>Was meinst du damit genau?<br />
Ich habe kompiliert mit</p>
<pre><code>g++ -O3 -std=c++11 -pthread code.cpp -o out
</code></pre>
<p>Wenn ich die asyncs mit std::launch::async starte, dann erstellt er auch verschiedene Threads. Mit std::launch::async|std::launch::deferred (also dem default) halt nicht. Vielleicht erkennt der Compiler, dass es in dem Fall keinen Sinn hat, separate Threads zu starten? (womit er ja auch recht hätte :D)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2414783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2414783</guid><dc:creator><![CDATA[asdfasd]]></dc:creator><pubDate>Sun, 24 Aug 2014 19:36:42 GMT</pubDate></item><item><title><![CDATA[Reply to Exception fliegt in Testprogramm zu std::async on Sun, 24 Aug 2014 20:57:29 GMT]]></title><description><![CDATA[<p>Laut cppreference,com ist das verhalten normal</p>
<blockquote>
<p>If both the std::launch::async and std::launch::deferred flags are set in policy, it is up to the implementation whether to perform asynchronous execution or lazy evaluation.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2414790</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2414790</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sun, 24 Aug 2014 20:57:29 GMT</pubDate></item></channel></rss>