<?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[Thread Syncronisation]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hätte dann noch eine Frage zur Thread Syncronisation.</p>
<pre><code class="language-cpp">#ifndef CONSUMER_H
#define CONSUMER_H
#include &lt;iostream&gt;
#include&quot;Ablage.h&quot;

class Consumer {
public:
   Consumer(Ablage&amp; a)
      : ablage(a) {
   }
   void operator()() {
      try {
         while(true) {                // Abbruch mit Exception
            int wert = ablage.get();
            boost::lock_guard&lt;boost::mutex&gt; lock(ausgabeMutex);
            std::cout &lt;&lt; &quot;Consumer hat &quot; 
                      &lt;&lt; wert &lt;&lt; &quot; geholt.&quot; &lt;&lt; std::endl;
         }
      } catch(...) {
         boost::lock_guard&lt;boost::mutex&gt; lock(ausgabeMutex);
         std::cout &lt;&lt; &quot;Consumer beendet sich.&quot; &lt;&lt; std::endl; 
      }
   }
private:
   Ablage&amp; ablage;
};

#endif
</code></pre>
<pre><code class="language-cpp">#ifndef PRODUCER_H
#define PRODUCER_H
#include &lt;iostream&gt;
#include&lt;Random.h&gt;
#include&quot;Ablage.h&quot;

class Producer {
public:
   Producer(Ablage&amp; a, int i)
      : ablage(a), id(i), zufall(500) {
   }
   void operator()() {
      for(int i = 0; i &lt; 5; ++i) {
         int wert = id*10 + i;
         boost::this_thread::sleep(
                      boost::posix_time::millisec(200 + zufall()));
         try {
            ablage.put(wert);
            boost::lock_guard&lt;boost::mutex&gt; lock(ausgabeMutex);
            std::cout &lt;&lt; &quot;Producer Nr. &quot; &lt;&lt; id 
                      &lt;&lt; &quot;  legt ab: &quot; &lt;&lt; wert &lt;&lt; std::endl;
         } catch(...) {
              break; //  Producer ist beendet
         }
      }
      boost::lock_guard&lt;boost::mutex&gt; lock(ausgabeMutex);
      std::cout &lt;&lt; &quot;Producer &quot; &lt;&lt; id &lt;&lt; &quot; beendet sich.&quot; &lt;&lt; std::endl; 
   }
private:
   Ablage&amp; ablage;
   int id;
   Random zufall;
};

#endif
</code></pre>
<p>Wieso muss die Ausgabe mit Mutex abgesichert werden? ich dachte immer das verwendet man, wenn man gemeinsame Variablen nutzt? Oder ist hier cout die gemeinsame Variable und es könnten so die Buchstaben gemixt werden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/263572/thread-syncronisation</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 16:10:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/263572.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 22 Mar 2010 20:55:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Thread Syncronisation on Mon, 22 Mar 2010 20:55:04 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hätte dann noch eine Frage zur Thread Syncronisation.</p>
<pre><code class="language-cpp">#ifndef CONSUMER_H
#define CONSUMER_H
#include &lt;iostream&gt;
#include&quot;Ablage.h&quot;

class Consumer {
public:
   Consumer(Ablage&amp; a)
      : ablage(a) {
   }
   void operator()() {
      try {
         while(true) {                // Abbruch mit Exception
            int wert = ablage.get();
            boost::lock_guard&lt;boost::mutex&gt; lock(ausgabeMutex);
            std::cout &lt;&lt; &quot;Consumer hat &quot; 
                      &lt;&lt; wert &lt;&lt; &quot; geholt.&quot; &lt;&lt; std::endl;
         }
      } catch(...) {
         boost::lock_guard&lt;boost::mutex&gt; lock(ausgabeMutex);
         std::cout &lt;&lt; &quot;Consumer beendet sich.&quot; &lt;&lt; std::endl; 
      }
   }
private:
   Ablage&amp; ablage;
};

#endif
</code></pre>
<pre><code class="language-cpp">#ifndef PRODUCER_H
#define PRODUCER_H
#include &lt;iostream&gt;
#include&lt;Random.h&gt;
#include&quot;Ablage.h&quot;

class Producer {
public:
   Producer(Ablage&amp; a, int i)
      : ablage(a), id(i), zufall(500) {
   }
   void operator()() {
      for(int i = 0; i &lt; 5; ++i) {
         int wert = id*10 + i;
         boost::this_thread::sleep(
                      boost::posix_time::millisec(200 + zufall()));
         try {
            ablage.put(wert);
            boost::lock_guard&lt;boost::mutex&gt; lock(ausgabeMutex);
            std::cout &lt;&lt; &quot;Producer Nr. &quot; &lt;&lt; id 
                      &lt;&lt; &quot;  legt ab: &quot; &lt;&lt; wert &lt;&lt; std::endl;
         } catch(...) {
              break; //  Producer ist beendet
         }
      }
      boost::lock_guard&lt;boost::mutex&gt; lock(ausgabeMutex);
      std::cout &lt;&lt; &quot;Producer &quot; &lt;&lt; id &lt;&lt; &quot; beendet sich.&quot; &lt;&lt; std::endl; 
   }
private:
   Ablage&amp; ablage;
   int id;
   Random zufall;
};

#endif
</code></pre>
<p>Wieso muss die Ausgabe mit Mutex abgesichert werden? ich dachte immer das verwendet man, wenn man gemeinsame Variablen nutzt? Oder ist hier cout die gemeinsame Variable und es könnten so die Buchstaben gemixt werden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1872711</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1872711</guid><dc:creator><![CDATA[Thomas22]]></dc:creator><pubDate>Mon, 22 Mar 2010 20:55:04 GMT</pubDate></item><item><title><![CDATA[Reply to Thread Syncronisation on Mon, 22 Mar 2010 21:06:43 GMT]]></title><description><![CDATA[<p>Jap. cout ist geshared und wird nicht synchronisiert. Kann also gut sein, wenn es nicht synchronisiert wird, dass Buchstaben durcheinander kommen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1872720</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1872720</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 22 Mar 2010 21:06:43 GMT</pubDate></item><item><title><![CDATA[Reply to Thread Syncronisation on Tue, 23 Mar 2010 01:47:02 GMT]]></title><description><![CDATA[<p>da der aktuelle C++ standard keine threads kennt, gibt es auch keinerlei garnatien bezüglich cout.</p>
<p>eine vernünftige implementierung wird zwar niemals &quot;bab&quot; ausgeben wenn ein thread &lt;&lt; &quot;a&quot; macht und der andere &lt;&lt; &quot;bb&quot;.</p>
<p>allerdings kann man IMO nicht standardkonform sicherstellen, dass nicht &quot;bab&quot; rauskommt, wenn der 2. thread &lt;&lt; &quot;b&quot; &lt;&lt; &quot;b&quot; machen würde statt &lt;&lt; &quot;bb&quot;.</p>
<p>und da der standard wie gesagt überhaupt garkeine garantien gibt, kann man sich nichtmal drauf verlassen dass cout überhaupt aus zwei threads gleichzeitig verwendet werden darf. es ist zwar üblich dass es geht, aber naja.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1872777</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1872777</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 23 Mar 2010 01:47:02 GMT</pubDate></item><item><title><![CDATA[Reply to Thread Syncronisation on Tue, 23 Mar 2010 12:38:47 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>eine vernünftige implementierung wird zwar niemals &quot;bab&quot; ausgeben wenn ein thread &lt;&lt; &quot;a&quot; macht und der andere &lt;&lt; &quot;bb&quot;.</p>
</blockquote>
<p>Wieso das denn nicht? Wenn <code>cout</code> ganz innen drin <code>putc</code> oder Ähnliches verwendet, kann ein anderer Thread doch zwischen zwei Aufrufen sein Zeichen ausgeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1872911</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1872911</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Tue, 23 Mar 2010 12:38:47 GMT</pubDate></item><item><title><![CDATA[Reply to Thread Syncronisation on Tue, 23 Mar 2010 14:24:04 GMT]]></title><description><![CDATA[<p>TyRoXx schrieb:</p>
<blockquote>
<p>hustbaer schrieb:</p>
<blockquote>
<p>eine vernünftige implementierung wird zwar niemals &quot;bab&quot; ausgeben wenn ein thread &lt;&lt; &quot;a&quot; macht und der andere &lt;&lt; &quot;bb&quot;.</p>
</blockquote>
<p>Wieso das denn nicht? Wenn <code>cout</code> ganz innen drin <code>putc</code> oder Ähnliches verwendet, kann ein anderer Thread doch zwischen zwei Aufrufen sein Zeichen ausgeben.</p>
</blockquote>
<p>hustbaer schrieb:</p>
<blockquote>
<p>eine <em><strong>vernünftige</strong></em> implementierung wird zwar niemals &quot;bab&quot; ausgeben wenn ein thread &lt;&lt; &quot;a&quot; macht und der andere &lt;&lt; &quot;bb&quot;.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1872963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1872963</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 23 Mar 2010 14:24:04 GMT</pubDate></item><item><title><![CDATA[Reply to Thread Syncronisation on Tue, 23 Mar 2010 20:01:02 GMT]]></title><description><![CDATA[<p>Ich müsste mich jetzt sehr täuschen, aber ich habe das verhalten bei MSVC++ auch schon beobachtet..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1873068</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1873068</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Tue, 23 Mar 2010 20:01:02 GMT</pubDate></item><item><title><![CDATA[Reply to Thread Syncronisation on Tue, 23 Mar 2010 23:31:19 GMT]]></title><description><![CDATA[<p>Microsoft schreibt bei ANSI-C (putc, printf, u.s.w.) ganz unverholen hin, das<br />
diese Funktionen NICHT threadsafe sind.</p>
<p>Geht ab Dual-Core auch wirklich fast immer schief.</p>
<p>Das es auch anders gehen könnte kann man bei UNIX (Solaris, Linux, ..) sehen.</p>
<p>VG<br />
merano</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1873122</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1873122</guid><dc:creator><![CDATA[merano]]></dc:creator><pubDate>Tue, 23 Mar 2010 23:31:19 GMT</pubDate></item></channel></rss>