<?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[Producer Consumer Problem]]></title><description><![CDATA[<p>hi, brauche ich beim folgenden beispiel: Producer, Consumer Problem noch einen weiteren lock? wie funktioniert cv.wait(l, [this](){ return !produced; } ); // 1 genau?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;thread&gt;
#include &lt;mutex&gt;
#include &lt;condition_variable&gt;
using namespace std;

class ProducerConsumer {
private:
    int value; // 1
    bool produced; // 2
    mutex m;
	condition_variable cv;

public:
	ProducerConsumer(): value(-1), produced(false) {}

	void producer(unsigned int count) {
        for(int i = count; i &gt;= 0; --i) {
            unique_lock&lt;mutex&gt; l(m);
            cv.wait(l, [this](){ return !produced; } ); // 1

            cout &lt;&lt; &quot;Producer sets value to &quot; &lt;&lt; i &lt;&lt; endl;
            value = i;
            produced = true;
            cv.notify_one(); // 2
        }
    }

	void consumer() {
	    do {
            unique_lock&lt;mutex&gt; l(m);
            cv.wait(l, [this](){ return produced; } );

            cout &lt;&lt; &quot;Consumer now is in control: &quot; &lt;&lt; value &lt;&lt; endl;
            produced = false;
            cv.notify_one();
        } while(value);
	}
};

int main() {
	// your code goes here

	ProducerConsumer p;
	thread t1(&amp;ProducerConsumer::consumer, &amp;p);
	thread t2(&amp;ProducerConsumer::producer, &amp;p, 2);

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

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/323497/producer-consumer-problem</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 22:21:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323497.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 02 Feb 2014 06:03:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Producer Consumer Problem on Sun, 02 Feb 2014 06:03:18 GMT]]></title><description><![CDATA[<p>hi, brauche ich beim folgenden beispiel: Producer, Consumer Problem noch einen weiteren lock? wie funktioniert cv.wait(l, [this](){ return !produced; } ); // 1 genau?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;thread&gt;
#include &lt;mutex&gt;
#include &lt;condition_variable&gt;
using namespace std;

class ProducerConsumer {
private:
    int value; // 1
    bool produced; // 2
    mutex m;
	condition_variable cv;

public:
	ProducerConsumer(): value(-1), produced(false) {}

	void producer(unsigned int count) {
        for(int i = count; i &gt;= 0; --i) {
            unique_lock&lt;mutex&gt; l(m);
            cv.wait(l, [this](){ return !produced; } ); // 1

            cout &lt;&lt; &quot;Producer sets value to &quot; &lt;&lt; i &lt;&lt; endl;
            value = i;
            produced = true;
            cv.notify_one(); // 2
        }
    }

	void consumer() {
	    do {
            unique_lock&lt;mutex&gt; l(m);
            cv.wait(l, [this](){ return produced; } );

            cout &lt;&lt; &quot;Consumer now is in control: &quot; &lt;&lt; value &lt;&lt; endl;
            produced = false;
            cv.notify_one();
        } while(value);
	}
};

int main() {
	// your code goes here

	ProducerConsumer p;
	thread t1(&amp;ProducerConsumer::consumer, &amp;p);
	thread t2(&amp;ProducerConsumer::producer, &amp;p, 2);

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

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2381082</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381082</guid><dc:creator><![CDATA[algoman1]]></dc:creator><pubDate>Sun, 02 Feb 2014 06:03:18 GMT</pubDate></item><item><title><![CDATA[Reply to Producer Consumer Problem on Sun, 02 Feb 2014 08:49:32 GMT]]></title><description><![CDATA[<p>warum beenden thread1 und thread2 nicht wenn ich 2 consumer hinzufuege?</p>
<pre><code>#include &lt;iostream&gt; 
#include &lt;thread&gt; 
#include &lt;mutex&gt; 
#include &lt;condition_variable&gt; 
using namespace std;

/* Question:

*/

class ProducerConsumer {
private:
	int value; // 1 
	bool produced; // 2 
	mutex m;
	condition_variable cv;	

public:
	ProducerConsumer() : value(-1), produced(false) {}

	void producer(unsigned int count) {
		for (int i = count; i &gt;= 0; --i) {
			unique_lock&lt;mutex&gt; l(m);
			cv.wait(l, [this](){ return !produced; }); // 1 

			cout &lt;&lt; &quot;Producer sets value to &quot; &lt;&lt; i &lt;&lt; endl;

			value = i;
			produced = true;
			cv.notify_one(); // 2 
		}
	}

	void consumer(unsigned int thread_id) {
		do {
			unique_lock&lt;mutex&gt; l(m);
			cv.wait(l, [this](){ return produced; });

			cout &lt;&lt; &quot;Consumer thread &quot; &lt;&lt; thread_id &lt;&lt; &quot;...now is in control: &quot; &lt;&lt; value &lt;&lt; endl;
			produced = false;
			cv.notify_one();
		} while (value);

		cout &lt;&lt; thread_id &lt;&lt; endl;
	}
};

int main() {
	// your code goes here 

	ProducerConsumer p;
	thread t1(&amp;ProducerConsumer::consumer, &amp;p, 1);
	thread t2(&amp;ProducerConsumer::consumer, &amp;p, 2);
	thread t3(&amp;ProducerConsumer::producer, &amp;p, 3);

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

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2381086</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381086</guid><dc:creator><![CDATA[algoman1]]></dc:creator><pubDate>Sun, 02 Feb 2014 08:49:32 GMT</pubDate></item><item><title><![CDATA[Reply to Producer Consumer Problem on Sun, 02 Feb 2014 17:47:04 GMT]]></title><description><![CDATA[<p>Any idea?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2381160</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381160</guid><dc:creator><![CDATA[Algorithman1]]></dc:creator><pubDate>Sun, 02 Feb 2014 17:47:04 GMT</pubDate></item><item><title><![CDATA[Reply to Producer Consumer Problem on Mon, 03 Feb 2014 00:09:49 GMT]]></title><description><![CDATA[<p>problem gefunden, ein consumer thread musste im wait unendlich lange warten...<br />
warum bekommen jetzt manchmal 2 consumer threads gleichzeitig access?</p>
<pre><code>#include &lt;iostream&gt; 
#include &lt;thread&gt; 
#include &lt;mutex&gt; 
#include &lt;condition_variable&gt; 
using namespace std;

/* Question:

*/

class ProducerConsumer {
private:
	int value; // 1 
	bool produced; // 2 
	mutex lock;
	mutex m;
	condition_variable cv;

public:
	ProducerConsumer() : value(-1), produced(false) {}

	void producer(unsigned int count) {
		for (int i = count; i &gt;= 0; --i) {
			{
				unique_lock&lt;mutex&gt; l(lock);
				cv.wait(l, [this](){ return !produced; }); // 1
			}

			{
				unique_lock&lt;mutex&gt; l(m);
				cout &lt;&lt; &quot;Producer sets value to &quot; &lt;&lt; i &lt;&lt; endl;
			}

			value = i;
			produced = true;
			cv.notify_one(); // 2 
		}
	}

	void consumer(unsigned int thread_id) {
		do {
			bool exit_loop = false;

			{
				unique_lock&lt;mutex&gt; l(lock);

				if (!cv.wait_for(l, std::chrono::milliseconds(500), [this](){ return produced; })) {
					std::cerr &lt;&lt; &quot;Thread &quot; &lt;&lt; thread_id &lt;&lt; &quot; finished waiting&quot; &lt;&lt; endl;
					exit_loop = true;
					break;
				}
			}

			if (exit_loop) {
				break;
			}

			{
				unique_lock&lt;mutex&gt; l(m);
				cout &lt;&lt; &quot;Consumer thread &quot; &lt;&lt; thread_id &lt;&lt; &quot;...now is in control: &quot; &lt;&lt; value &lt;&lt; endl;
			}

			produced = false;
			cv.notify_one();

			std::this_thread::sleep_for(std::chrono::milliseconds(10));
		} while (value);
	}
};

int main() {
	// your code goes here 

	ProducerConsumer p;
	thread consumer_thread1(&amp;ProducerConsumer::consumer, &amp;p, 1);
	thread consumer_thread2(&amp;ProducerConsumer::consumer, &amp;p, 2);
	thread producer_thread(&amp;ProducerConsumer::producer, &amp;p, 10);

	producer_thread.join();
	consumer_thread1.join();
	consumer_thread2.join();

	cout &lt;&lt; &quot;\nThreads successfully completed...&quot; &lt;&lt; endl;

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2381219</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381219</guid><dc:creator><![CDATA[algoman1]]></dc:creator><pubDate>Mon, 03 Feb 2014 00:09:49 GMT</pubDate></item><item><title><![CDATA[Reply to Producer Consumer Problem on Mon, 03 Feb 2014 00:10:38 GMT]]></title><description><![CDATA[<p>beispiel:</p>
<pre><code>Producer sets value to 10
Consumer thread 2...now is in control: 10
Producer sets value to 9
Consumer thread 1...now is in control: 9
Consumer thread 2...now is in control: 9
Producer sets value to 8
Consumer thread 1...now is in control: 8
Consumer thread 2...now is in control: 8
Producer sets value to 7
Consumer thread 1...now is in control: 7
Producer sets value to 6
Consumer thread 2...now is in control: 6
Producer sets value to 5
Consumer thread 1...now is in control: 5
Producer sets value to 4
Consumer thread 2...now is in control: 4
Producer sets value to 3
Consumer thread 1...now is in control: 3
Producer sets value to 2
Consumer thread 2...now is in control: 2
Producer sets value to 1
Consumer thread 1...now is in control: 1
Producer sets value to 0
Consumer thread 2...now is in control: 0

Threads successfully completed...
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2381220</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381220</guid><dc:creator><![CDATA[algoman1]]></dc:creator><pubDate>Mon, 03 Feb 2014 00:10:38 GMT</pubDate></item><item><title><![CDATA[Reply to Producer Consumer Problem on Mon, 03 Feb 2014 01:15:56 GMT]]></title><description><![CDATA[<p>algoman1 schrieb:</p>
<blockquote>
<p>...warum bekommen jetzt manchmal 2 consumer threads gleichzeitig access?</p>
</blockquote>
<p>Der unique_lock (Z.45) ist nur in dem scope in dem er definiert ist wirksam (Z.45 - 52). Der zweite lock (m) ist überflüssig, doppelt hält nicht besser <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/2381225</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381225</guid><dc:creator><![CDATA[osdt]]></dc:creator><pubDate>Mon, 03 Feb 2014 01:15:56 GMT</pubDate></item></channel></rss>