<?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[sinnfreies beispiel umbauen, mit condition_variable]]></title><description><![CDATA[<p>wie kann ich folgendes beispiel mit condition_variable machen ?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;thread&gt;

auto work1(volatile int &amp;run) -&gt; void
{
   const int wait_seconds = 1000;
   int waited = 0;

   while(run != 0)
   {
      while(waited &lt; wait_seconds &amp;&amp; run != 0)
      {
         _sleep(500);
         waited += 500;
      }
      if(run != 0)
      {
         std::cout &lt;&lt; &quot;do my job1...\n&quot;;
         waited = 0;
      }
   }
}

auto work2(volatile int &amp;run) -&gt; void
{
   const int wait_seconds = 1500;
   int waited = 0;

   while(run != 0)
   {
      while(waited &lt; wait_seconds &amp;&amp; run != 0)
      {
         _sleep(500);
         waited += 500;
      }
      if(run != 0)
      {
         std::cout &lt;&lt; &quot;do my job2...\n&quot;;
         waited = 0;
      }
   }
}

auto main(int params_count, char **params) -&gt; int
{
   int run = 1;
   std::cout &lt;&lt; &quot;erstelle thread1\n&quot;;
   std::thread thread1([&amp;run](void) { work1(run); });
   std::cout &lt;&lt; &quot;erstelle thread2\n&quot;;
   std::thread thread2([&amp;run](void) { work2(run); });

   std::cout &lt;&lt; &quot;sleep 5 seconds\n&quot;;
   _sleep(5000);

   std::cout &lt;&lt; &quot;programm wird beendet\n&quot;;
   run = 0;

   thread1.join();
   thread2.join();

   std::cout &lt;&lt; &quot;exit app\n&quot;;

   return 0;
}
</code></pre>
<p>die beiden threads warten also unterschiedlich lange um ihre arbeit im interval zu erledigen, bzw. gehen raus, wenn run gleich 0 ist.<br />
wie mach ich das mit condition_variable ?</p>
<p>noch ne frage zu meinem beispiel: theoretisch duerfte hier nichts passieren oder ? da es nur einen thread gibt der die variable run aendert.</p>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/338204/sinnfreies-beispiel-umbauen-mit-condition_variable</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Apr 2026 20:04:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/338204.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 30 May 2016 19:08:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to sinnfreies beispiel umbauen, mit condition_variable on Mon, 30 May 2016 19:08:56 GMT]]></title><description><![CDATA[<p>wie kann ich folgendes beispiel mit condition_variable machen ?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;thread&gt;

auto work1(volatile int &amp;run) -&gt; void
{
   const int wait_seconds = 1000;
   int waited = 0;

   while(run != 0)
   {
      while(waited &lt; wait_seconds &amp;&amp; run != 0)
      {
         _sleep(500);
         waited += 500;
      }
      if(run != 0)
      {
         std::cout &lt;&lt; &quot;do my job1...\n&quot;;
         waited = 0;
      }
   }
}

auto work2(volatile int &amp;run) -&gt; void
{
   const int wait_seconds = 1500;
   int waited = 0;

   while(run != 0)
   {
      while(waited &lt; wait_seconds &amp;&amp; run != 0)
      {
         _sleep(500);
         waited += 500;
      }
      if(run != 0)
      {
         std::cout &lt;&lt; &quot;do my job2...\n&quot;;
         waited = 0;
      }
   }
}

auto main(int params_count, char **params) -&gt; int
{
   int run = 1;
   std::cout &lt;&lt; &quot;erstelle thread1\n&quot;;
   std::thread thread1([&amp;run](void) { work1(run); });
   std::cout &lt;&lt; &quot;erstelle thread2\n&quot;;
   std::thread thread2([&amp;run](void) { work2(run); });

   std::cout &lt;&lt; &quot;sleep 5 seconds\n&quot;;
   _sleep(5000);

   std::cout &lt;&lt; &quot;programm wird beendet\n&quot;;
   run = 0;

   thread1.join();
   thread2.join();

   std::cout &lt;&lt; &quot;exit app\n&quot;;

   return 0;
}
</code></pre>
<p>die beiden threads warten also unterschiedlich lange um ihre arbeit im interval zu erledigen, bzw. gehen raus, wenn run gleich 0 ist.<br />
wie mach ich das mit condition_variable ?</p>
<p>noch ne frage zu meinem beispiel: theoretisch duerfte hier nichts passieren oder ? da es nur einen thread gibt der die variable run aendert.</p>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2497497</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2497497</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Mon, 30 May 2016 19:08:56 GMT</pubDate></item><item><title><![CDATA[Reply to sinnfreies beispiel umbauen, mit condition_variable on Mon, 30 May 2016 20:30:26 GMT]]></title><description><![CDATA[<p>Du machst dir z.B. ne struct wo deine &quot;run&quot; Variable (ohne volatile) drinnen ist, und ne Mutex, und ne Condition-Variable.<br />
Dann kannst du nen Zeiger bzw. ne Referenz auf diese struct an die Threads übergeben.</p>
<p>Überall wo du auf die &quot;run&quot; Variable zugreifst (lesend oder schreibend) musst du nun die Mutex locken.</p>
<p>Wenn du warten willst bis sich der Inhalt der Variable ändert, dann verwendest du <code>wait</code> der Condition-Variable. Wenn du dabei max. so-und-so lange warten willst, dann nimmst du <code>wait_for</code> . Bzw. <code>wait_until</code> wenn du ne absolute Deadline hast. Wenn du die Variable änderst, dann rufst du <code>notify_all</code> auf.</p>
<p>In deinem Fall ist vermutlich ne absolute Deadline mit der <code>std::steady_clock</code> angesagt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2497508</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2497508</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 30 May 2016 20:30:26 GMT</pubDate></item></channel></rss>