<?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[Threads und this_thread::sleeep_for]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe folgende Aufgabe zu meinem Buch versucht:</p>
<p>&quot;Write a program with two threads: one that writes hello every second and one that<br />
writes world! every second&quot;</p>
<p>Meine bisherige Lösung:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;chrono&gt;
#include &lt;thread&gt;

void hello()
{
    std::cout &lt;&lt; &quot;Hello\n&quot;;
}

void world()
{
    std::cout &lt;&lt; &quot;World\n&quot;;
}

int main()
{
    std::thread t1{hello};
    std::thread t2{world};

    //while (true)
    //{
        t1.join();
        //std::this_thread::sleep_for(std::chrono::seconds(1));
        t2.join();
    //}
    return 0;
}
</code></pre>
<p>Ich bekomme aber einen std::system_error -&gt; what(): Invalid Argument</p>
<p>ich weiß im Prinzip halt nicht, wie ich dieses je eine Sekunde schlafen anwenden muss, ist mir aus dem Buch nicht ersichtlich.</p>
<p>Code Beispiel aus dem Buch:</p>
<pre><code>using namespace std::chrono;

auto t0 = high_resolution_clock::now();
this_thread::sleep_for(milliseconds{20});
auto t1 = high_resolution_clock::now();
cout &lt;&lt; duration_cast&lt;nanonseconds&gt;(t1 - t0).count() &lt;&lt; &quot; nanoseconds passed\n&quot;;
</code></pre>
<p>habe schon bemerkt, dass es an dem thread.join() innerhalb der while(true) Schleife liegt. Weiß aber nicht, wie ich es sonst mehrmals ausgeben lassen soll</p>
<p>Hoffe ihr könnt mir helfen (bzw. tut es, können bestimmt^^)</p>
<p>LG<br />
HarteWare</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/324593/threads-und-this_thread-sleeep_for</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 10:50:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/324593.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 23 Mar 2014 17:16:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Threads und this_thread::sleeep_for on Sun, 23 Mar 2014 17:16:15 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe folgende Aufgabe zu meinem Buch versucht:</p>
<p>&quot;Write a program with two threads: one that writes hello every second and one that<br />
writes world! every second&quot;</p>
<p>Meine bisherige Lösung:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;chrono&gt;
#include &lt;thread&gt;

void hello()
{
    std::cout &lt;&lt; &quot;Hello\n&quot;;
}

void world()
{
    std::cout &lt;&lt; &quot;World\n&quot;;
}

int main()
{
    std::thread t1{hello};
    std::thread t2{world};

    //while (true)
    //{
        t1.join();
        //std::this_thread::sleep_for(std::chrono::seconds(1));
        t2.join();
    //}
    return 0;
}
</code></pre>
<p>Ich bekomme aber einen std::system_error -&gt; what(): Invalid Argument</p>
<p>ich weiß im Prinzip halt nicht, wie ich dieses je eine Sekunde schlafen anwenden muss, ist mir aus dem Buch nicht ersichtlich.</p>
<p>Code Beispiel aus dem Buch:</p>
<pre><code>using namespace std::chrono;

auto t0 = high_resolution_clock::now();
this_thread::sleep_for(milliseconds{20});
auto t1 = high_resolution_clock::now();
cout &lt;&lt; duration_cast&lt;nanonseconds&gt;(t1 - t0).count() &lt;&lt; &quot; nanoseconds passed\n&quot;;
</code></pre>
<p>habe schon bemerkt, dass es an dem thread.join() innerhalb der while(true) Schleife liegt. Weiß aber nicht, wie ich es sonst mehrmals ausgeben lassen soll</p>
<p>Hoffe ihr könnt mir helfen (bzw. tut es, können bestimmt^^)</p>
<p>LG<br />
HarteWare</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2390620</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2390620</guid><dc:creator><![CDATA[HarteWare]]></dc:creator><pubDate>Sun, 23 Mar 2014 17:16:15 GMT</pubDate></item><item><title><![CDATA[Reply to Threads und this_thread::sleeep_for on Sun, 23 Mar 2014 17:37:28 GMT]]></title><description><![CDATA[<p>Ist die Idee nicht, <em>innerhalb</em> der Threads eine Schleife zu haben und zu schlafen? Nachdem du die Threads startest, hast du ja in <code>main()</code> keine direkte Kontrolle mehr über ihren Programmablauf.</p>
<p>Und musst du das synchronisieren, sodass &quot;Hello&quot; ständig vor &quot;World&quot; ausgegeben wird?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2390625</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2390625</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 23 Mar 2014 17:37:28 GMT</pubDate></item><item><title><![CDATA[Reply to Threads und this_thread::sleeep_for on Mon, 24 Mar 2014 13:02:41 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/12954">@Nexus</a><br />
Das ist wirklich die ganze Aufgabenstellung gewesen, also ist da wohl Spielraum für Interpretation. Bei meinem PC funktionierts ohne jegliche explizite Syncronisation, sprich es erscheint immer 'Hello' for 'World', also kann ich mir das Arbeiten mit mutex und lock erstmal ersparen, oder was man da halt so benötigen würde.</p>
<p>Jedenfalls ist mir mein Hauptdenkfehler jetzt bewusst geworden, und es funktioniert wie es soll, danke <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Falls es jemanden kümmert, so siehts jetzt aus:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;chrono&gt;
#include &lt;thread&gt;

void hello()
{
    while(true)
    {
        std::cout &lt;&lt; &quot;Hello &quot;;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

void world()
{
    while(true)
    {
        std::cout &lt;&lt; &quot;World!\n&quot;;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

int main()
{
    std::thread t1{hello};
    std::thread t2{world};

    t1.join();
    t2.join();

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2390767</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2390767</guid><dc:creator><![CDATA[HarteWare]]></dc:creator><pubDate>Mon, 24 Mar 2014 13:02:41 GMT</pubDate></item><item><title><![CDATA[Reply to Threads und this_thread::sleeep_for on Mon, 24 Mar 2014 13:19:22 GMT]]></title><description><![CDATA[<p>HarteWare schrieb:</p>
<blockquote>
<p>Bei meinem PC funktionierts ohne jegliche explizite Syncronisation</p>
</blockquote>
<p>Dir sollte klar sein, dass das nur zufällig gut geht. Minimale Änderungen (zum Beispiel dass ein Flash-Video im Hintergrund läuft) können dir alles kaputt machen. Wahrscheinlich war der Sinn der Aufgabe, dass du siehst, dass manchmal &quot;Hello&quot; vor &quot;World&quot; kommt und manchmal andersrum, als Motivation wozu man Mutexe braucht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2390777</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2390777</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Mon, 24 Mar 2014 13:19:22 GMT</pubDate></item><item><title><![CDATA[Reply to Threads und this_thread::sleeep_for on Mon, 24 Mar 2014 13:49:36 GMT]]></title><description><![CDATA[<p>Ja, is mir klar. Nur so nebenbei, wäre es so richtig? Was wäre die &quot;optimale Lösung&quot;? Für so ein kleines Programm sollte es eine geben, oder?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;chrono&gt;
#include &lt;thread&gt;
#include &lt;mutex&gt;

std::mutex m;

void hello()
{
    while(true)
    {
        std::unique_lock&lt;std::mutex&gt; lck{m};
        std::cout &lt;&lt; &quot;Hello &quot;;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

void world()
{
    while(true)
    {
        std::unique_lock&lt;std::mutex&gt; lck{m};
        std::cout &lt;&lt; &quot;World!\n&quot;;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

int main()
{
    std::thread t1{hello};
    std::thread t2{world};

    t1.join();
    t2.join();

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2390782</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2390782</guid><dc:creator><![CDATA[HarteWare]]></dc:creator><pubDate>Mon, 24 Mar 2014 13:49:36 GMT</pubDate></item><item><title><![CDATA[Reply to Threads und this_thread::sleeep_for on Mon, 24 Mar 2014 14:00:20 GMT]]></title><description><![CDATA[<p>Der Lock nützt dir gar nichts. Er garantiert zwar, dass die Threads nicht gleichzeitig auf cout zugreifen können, aber nicht, dass hello vor world dran kommt. cout ist bereits threadsafe, daher hat selbst das keinen Effekt. Die Optimallösung hängt von den Anforderungen ab. Ich würde es optimal finden, wenn man die Threads weg lässt und jede Sekunde &quot;Hello World&quot; ausgibt. Dann kann garantiert nichts schief gehen. Ansonsten könnte man den Mutex in main locken, in world auch und in hello unlocken. Damit kann world nicht mehr vor hello dran kommen. Vorausgesetzt die sleep_for-Sache wird nie eine Sekunde driften. Um den Drift zu verhindern kannst du ihm sagen, dass er nicht eine Sekunde, sondern eine Sekunde minus Zeit der letzten Aktivierung schlafen soll. Aber ohne genauere Aufgabenstellung könnte ich nicht argumentieren welche Variante richtig wäre.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2390784</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2390784</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Mon, 24 Mar 2014 14:00:20 GMT</pubDate></item><item><title><![CDATA[Reply to Threads und this_thread::sleeep_for on Mon, 24 Mar 2014 14:07:16 GMT]]></title><description><![CDATA[<p>Nun gut, Aufgabenstellung ist wirklich exakt so (nur eine kleine Übung) : &quot;Write a program with two threads: one that writes hello every second and one that<br />
writes world! every second&quot;</p>
<p>Also mit Threads, und ich denke alles weitere wäre &quot;overkill&quot;, aber gut zu wissen, dass die locks so angewendet wenig Sinn machen, danke.</p>
<p>LG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2390786</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2390786</guid><dc:creator><![CDATA[HarteWare]]></dc:creator><pubDate>Mon, 24 Mar 2014 14:07:16 GMT</pubDate></item><item><title><![CDATA[Reply to Threads und this_thread::sleeep_for on Mon, 24 Mar 2014 14:15:45 GMT]]></title><description><![CDATA[<p>Dein Programm ist aber eines mit 3 Threads.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2390788</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2390788</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Mon, 24 Mar 2014 14:15:45 GMT</pubDate></item><item><title><![CDATA[Reply to Threads und this_thread::sleeep_for on Mon, 24 Mar 2014 14:30:39 GMT]]></title><description><![CDATA[<p>Lol, stimmt^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2390793</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2390793</guid><dc:creator><![CDATA[HarteWare]]></dc:creator><pubDate>Mon, 24 Mar 2014 14:30:39 GMT</pubDate></item></channel></rss>