<?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-safe producer&#x2F;consumer queue]]></title><description><![CDATA[<p>Ich schätze mal, dass die meisten, die mit Multithreading Kontakt hatten, auch irgendwann mal mit Message-Queues, Producer/Consumer Queues, Event Queues und derartigem zu tun hatten. Ich suche für mein Projekt grade eine möglichst lockfreie Implementierung für eine Eventqueue auf der Basis von Boost + MSVC's C++0x features.<br />
Dabei bin ich mir auch noch nicht ganz sicher, wie das Interface auf Consumer-Seite am Ende aussehen soll: blocking &quot;Event getEvent()&quot;, ggf. incl. &quot;bool hasEvent()&quot;, oder Übergabe eines Handlers und abarbeiten aller vorliegenden Events oder wie auch immer.<br />
Meine Rahmenbedingungen sind auf jeden Fall, dass es einen Producer und einen Cosumer geben soll - mehrere Consumer wären möglich, möchte ich aber erstmal nicht supporten.</p>
<p>Wer hat Erfahrungen mit sowas gemacht?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/294123/thread-safe-producer-consumer-queue</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 01:04:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/294123.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 18 Oct 2011 12:33:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 12:33:37 GMT]]></title><description><![CDATA[<p>Ich schätze mal, dass die meisten, die mit Multithreading Kontakt hatten, auch irgendwann mal mit Message-Queues, Producer/Consumer Queues, Event Queues und derartigem zu tun hatten. Ich suche für mein Projekt grade eine möglichst lockfreie Implementierung für eine Eventqueue auf der Basis von Boost + MSVC's C++0x features.<br />
Dabei bin ich mir auch noch nicht ganz sicher, wie das Interface auf Consumer-Seite am Ende aussehen soll: blocking &quot;Event getEvent()&quot;, ggf. incl. &quot;bool hasEvent()&quot;, oder Übergabe eines Handlers und abarbeiten aller vorliegenden Events oder wie auch immer.<br />
Meine Rahmenbedingungen sind auf jeden Fall, dass es einen Producer und einen Cosumer geben soll - mehrere Consumer wären möglich, möchte ich aber erstmal nicht supporten.</p>
<p>Wer hat Erfahrungen mit sowas gemacht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132824</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132824</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Tue, 18 Oct 2011 12:33:37 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 12:47:50 GMT]]></title><description><![CDATA[<p>Ich hatte gerade vor kurzem das Vergnügen, allerdings nicht mit C++. Programmiere die Queue selbst; es ist weniger Aufwand, als man meint.</p>
<p>Gib deiner Queue das normale Interface einer Queue; mach nichts spezielles draus. Für eine &quot;lockfreie&quot; Implementierung gibt es tausend Varianten. Ich würde empfehlen, mit Semaphore zu arbeiten. Du brauchst 3 davon: Einen Get-Semaphor, einen Put-Semaphor und einen Mutex für das Handling einer Critical Section.</p>
<p>Dann musst nur den Ausschluss korrekt programmieren: Beim Einfügen die Put-Semaphore acquiren und die Get-Semaphore releasen, beim Entfernen genau umgekehrt. Dazu jegliche Zugriffe auf den Inhalt selbst mit dem Mutex synchronisieren.</p>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132829</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132829</guid><dc:creator><![CDATA[*rant*]]></dc:creator><pubDate>Tue, 18 Oct 2011 12:47:50 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 12:45:53 GMT]]></title><description><![CDATA[<p>lockfrei: keine Ahnung. Geht das überhaupt? Also, auch ohne <a href="http://de.wikipedia.org/wiki/Spinlock" rel="nofollow">Spin-Locks</a>?</p>
<p>Wenn jemand eine Lösung parat hat, die auch ohne Spin-Locks auskommt, würde mich das interessieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132831</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132831</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Tue, 18 Oct 2011 12:45:53 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:08:06 GMT]]></title><description><![CDATA[<blockquote>
<p>einen Producer und einen Cosumer</p>
</blockquote>
<p>Gab es da nicht mal vor Jahren einen Artikel in DrDobbs. <a href="http://www.gotw.ca/publications/" rel="nofollow">http://www.gotw.ca/publications/</a> (2008, #EC15), das Rad muss nicht neu erfunden werden. Andere Artikel auf der Seite zu Multitasking sind vielleicht auch interessant. Wenn dir Software transactual memory zur verfuegung steht (als Lib beispielsweise), dann ist die Implementation trivial (wie Simon Peyton-Jones mal anmerkte).</p>
<blockquote>
<p>Geht das überhaupt?</p>
</blockquote>
<p>Ja, das Zauberwort heisst compare-and-swap (oder xchange, ... ).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132838</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132838</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:08:06 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:42:38 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<blockquote>
<p>Geht das [ohne Spin-Locks] überhaupt?</p>
</blockquote>
<p>Ja. das Zauberwort heisst compare-and-swap (oder xchange, ... ).</p>
</blockquote>
<p>Das ist mir natürlich ein Begriff. Die Frage ist nur: Kommst Du damit aus? (Edit: Was ich damit meine, kommst du ohne CAS-loops aus?)</p>
<p>Zitat aus dem einleitenden Teil von Herb Sutters <a href="http://drdobbs.com/cpp/211601363" rel="nofollow">Dr Dobbs Artikel</a>:</p>
<blockquote>
<p>[...] We'll use ordered atomic variables (C++0x atomic&lt;&gt;, Java/.NET volatile) directly instead of prefabricated mutexes, but functionally we're still writing <strong>spinlocks</strong>; we're just writing them by hand. Although this means <strong>it's not a purely &quot;lock-free&quot; or nonblocking algorithm</strong>, it's still quite concurrent because we'll arrange the code to still let multiple consumers and multiple producers make progress at the same time by arranging to do as much work as possible outside the small critical code region that updates the head and tail, respectively.</p>
</blockquote>
<p>Hmm... klingt erstmal widersprüchlich zu Deiner Antwort. Aber vielleicht muss ich auch erst mal den ganzen Artikel lesen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132845</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132845</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:42:38 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:26:49 GMT]]></title><description><![CDATA[<p>Soll ich dir jetzt die zig Paper raussuchen? Es geht. Was es bringt? Fraglich, bzw. anwendungsspezifisch. Beispielsweise hier: <a href="http://www.research.ibm.com/people/m/michael/podc-1996.pdf" rel="nofollow">http://www.research.ibm.com/people/m/michael/podc-1996.pdf</a>, habe das Paper aber nur ueberflogen. Ansonsten <a href="http://en.wikipedia.org/wiki/Non-blocking_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Non-blocking_algorithm</a> unter External links.</p>
<p>Leider oeffnet sich der Artikel grad bei mir nicht, deswegen kann ich nicht genau nachsehen, was er mit &quot;functionally&quot; Spinlocks mein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132848</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132848</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:26:49 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:15:38 GMT]]></title><description><![CDATA[<p>Lockfreies blocking getEvent() <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Versteh ich nicht.<br />
Ich habe für solche Geschiten eine sehr simple Queue mit<br />
<a href="http://www.boost.org/doc/libs/1_47_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref" rel="nofollow">boost::condition_variable</a>. Das ist wirklich sehr einfach. Ob das jetzt vielleicht ganz kompliziert noch vieel toller geht.. keine Ahnung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132849</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132849</guid><dc:creator><![CDATA[brotbernd]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:15:38 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:20:36 GMT]]></title><description><![CDATA[<p>&quot;Lockfreies blocking&quot; alias &quot;busy waiting&quot;. Wenn eine Critical Section eben kaum belegt ist bzw. schnell wieder freigegeben wird, dann lohnt ein Mutex nicht. Das lock und unlock erzeugt sehr viel Overhead, da das Betriebssystem mit involviert ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132850</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:20:36 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:24:01 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>Hmm... klingt erstmal widersprüchlich zu Deiner Antwort. Aber vielleicht muss ich auch erst mal den ganzen Artikel lesen...</p>
</blockquote>
<p>Die Frage ist wie du ein Spin Lock definierst. Du brauchst eine Art synchronisation. Wenn du es über CAS machst, hast du dennoch das Problem, dass gerade jetzt die Daten von einem anderen thread geschrieben wurden - das erkennt CAS und schreibt die Daten nochmal. Jetzt kann es natürlich sein dass wieder ein anderer Thread dich unterbrochen hat, CAS erkennt das und schreibt die Daten neu...</p>
<p>Im Prinzip ist das ein Spin Lock - nur dass er idR nie spint. Deshalb nennt man es gemeinhin Lock Free. Da du nie lockst (im Sinne von Context Switch) aber dennoch musst du irgendwie die Daten synchronisieren.</p>
<p>Ein herkömmlicher SpinLock lockt ja ohne Context Switch - deshalb &quot;Spin&quot;. Was CAS macht ist, einfach nochmal die Daten zu schreiben und zu verifizieren. Prinzipiell wartet man nie auf ein Ereignis wie bei einem herkömmlichen Lock sondern versucht solange ein Ergebnis zu erreichen bis man es geschafft hat.</p>
<p>PS:<br />
Danke an knivil - da war ein Tipfehler drin der die Logik etwas verdreht hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132851</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132851</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:24:01 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:41:31 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<p>Soll ich dir jetzt die zig Paper raussuchen?</p>
</blockquote>
<p>Das wär nett.</p>
<p>knivil schrieb:</p>
<blockquote>
<p>Es geht.</p>
</blockquote>
<p>Vielleicht. Vielleicht nicht. Nimm es nicht persönlich, dass ich die Wahrscheinlichkeit eines Irrtums Deinerseits höher als Du selbst einschätze. So groß ist die auch nicht wirklich. Aber sie ist nicht klein genug, dass ich Deine Aussage bei mir als Wahrheit abspeichern könnte.</p>
<p>knivil schrieb:</p>
<blockquote>
<p>&quot;Lockfreies blocking&quot; alias &quot;busy waiting&quot;.</p>
</blockquote>
<p>Watt'n Scherzkeks! Reden wir etwa aneinander vorbei?!</p>
<p>Edit: Es scheint so.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132852</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132852</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:41:31 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:22:19 GMT]]></title><description><![CDATA[<p>Shade Of Mine schrieb:</p>
<blockquote>
<p>Datei [..] Daten</p>
</blockquote>
<p>Es handelt sich um Daten. Nicht, das jemand von dem Fluechtigkeitsfehler verwirrt wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132853</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132853</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:22:19 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:24:17 GMT]]></title><description><![CDATA[<p>Der Link zu knivils Artikel geht übrigens wenn man das Komma am Schluss weglässt. <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2132854</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132854</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:24:17 GMT</pubDate></item><item><title><![CDATA[Reply to thread-safe producer&#x2F;consumer queue on Tue, 18 Oct 2011 13:34:53 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>knivil schrieb:</p>
<blockquote>
<blockquote>
<p>Geht das [ohne Spin-Locks] überhaupt?</p>
</blockquote>
<p>Ja. das Zauberwort heisst compare-and-swap (oder xchange, ... ).</p>
</blockquote>
<p>Das ist mir natürlich ein Begriff. Die Frage ist nur: Kommst Du damit aus? (Edit: Was ich damit meine, kommst du ohne loops aus?)</p>
<p>Zitat aus dem einleitenden Teil von Herb Sutters <a href="http://drdobbs.com/cpp/211601363" rel="nofollow">Dr Dobbs Artikel</a>:</p>
<blockquote>
<p>[...] We'll use ordered atomic variables (C++0x atomic&lt;&gt;, Java/.NET volatile) directly instead of prefabricated mutexes, but functionally we're still writing <strong>spinlocks</strong>; we're just writing them by hand. Although this means <strong>it's not a purely &quot;lock-free&quot; or nonblocking algorithm</strong>, it's still quite concurrent because we'll arrange the code to still let multiple consumers and multiple producers make progress at the same time by arranging to do as much work as possible outside the small critical code region that updates the head and tail, respectively.</p>
</blockquote>
<p>Hmm... klingt erstmal widersprüchlich zu Deiner Antwort. Aber vielleicht muss ich auch erst mal den ganzen Artikel lesen...</p>
</blockquote>
<p>Ich habe den Artikel nur mal quergelesen, ich denke das ist wirklich eine ganz normale Implementation mit Spin-locks, die nur versucht, etwas cleverer zu sein; so das möglicherweise die negativen Effekte (etwa dead-lock durch priority inversion) weniger wahrscheinlich sind.<br />
Eine wirklich lockfreie Liste ist tatsächlich eine recht komplizierte Angelegenheit. Ohne die Hilfe von <a href="http://en.wikipedia.org/wiki/Hazard_pointer" rel="nofollow">hazard pointern</a> wird es noch schwieriger, und DWCS ist bekanntlich auf vielene Architekturen nicht verfügbar.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132858</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132858</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 18 Oct 2011 13:34:53 GMT</pubDate></item></channel></rss>