<?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[Verhalten von Boost-Threads unter MS VS 2010]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mal foldendes Programm aus einem alten iX-Artikel eingegeben. Das Programm läuft auch problemlos. Nur ich verstehe das Verhalten nicht.</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;boost\thread.hpp&gt;

int counter;
const int NCOUNT = 100;
boost::mutex incrementMutex;

void increment(const int ncount)
{
	for (int i(0); i &lt; ncount; i++)
	{
		boost::lock_guard&lt;boost::mutex&gt; lock(incrementMutex);
		counter++;
		std::cout &lt;&lt; counter &lt;&lt; std::endl;
	}
}

void decrement(const int ncount)
{
	for (int i(0); i &lt; ncount; i++)
	{
		boost::lock_guard&lt;boost::mutex&gt; lock(incrementMutex);
		counter--;
		std::cout &lt;&lt; counter &lt;&lt; std::endl;
	}
}

int main(int argc, char* argv[])
{
	counter = 0;

	boost::thread a(std::bind(increment, NCOUNT));
	boost::thread b(std::bind(decrement, NCOUNT));

	a.join();
	b.join();

	std::cin.get();

	return 0;
}
</code></pre>
<p>Wenn ich das Programm starte zählt das Programm erst runter und dann rauf, oder auch andersrum. Ich hätte erwartet das sich der Wert der Variable counter kaum erhöht, weil beide Threads ungefähr gleichviel CPU-Zeit bekommen.</p>
<p>Etwas ratlos...</p>
<p>Ich verwende MS VS 2010 und die Boost-Library 1.54.0.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/326147/verhalten-von-boost-threads-unter-ms-vs-2010</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 09:30:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/326147.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 04 Jun 2014 08:49:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Verhalten von Boost-Threads unter MS VS 2010 on Wed, 04 Jun 2014 08:49:38 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mal foldendes Programm aus einem alten iX-Artikel eingegeben. Das Programm läuft auch problemlos. Nur ich verstehe das Verhalten nicht.</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;boost\thread.hpp&gt;

int counter;
const int NCOUNT = 100;
boost::mutex incrementMutex;

void increment(const int ncount)
{
	for (int i(0); i &lt; ncount; i++)
	{
		boost::lock_guard&lt;boost::mutex&gt; lock(incrementMutex);
		counter++;
		std::cout &lt;&lt; counter &lt;&lt; std::endl;
	}
}

void decrement(const int ncount)
{
	for (int i(0); i &lt; ncount; i++)
	{
		boost::lock_guard&lt;boost::mutex&gt; lock(incrementMutex);
		counter--;
		std::cout &lt;&lt; counter &lt;&lt; std::endl;
	}
}

int main(int argc, char* argv[])
{
	counter = 0;

	boost::thread a(std::bind(increment, NCOUNT));
	boost::thread b(std::bind(decrement, NCOUNT));

	a.join();
	b.join();

	std::cin.get();

	return 0;
}
</code></pre>
<p>Wenn ich das Programm starte zählt das Programm erst runter und dann rauf, oder auch andersrum. Ich hätte erwartet das sich der Wert der Variable counter kaum erhöht, weil beide Threads ungefähr gleichviel CPU-Zeit bekommen.</p>
<p>Etwas ratlos...</p>
<p>Ich verwende MS VS 2010 und die Boost-Library 1.54.0.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2402320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402320</guid><dc:creator><![CDATA[WiederMalamRaetseln]]></dc:creator><pubDate>Wed, 04 Jun 2014 08:49:38 GMT</pubDate></item><item><title><![CDATA[Reply to Verhalten von Boost-Threads unter MS VS 2010 on Wed, 04 Jun 2014 09:19:55 GMT]]></title><description><![CDATA[<p>Es ist nicht garantiert in welcher Reihenfolge die Threads Prozessorzeit bekommen (das hast du ja selbst festgestellt). Und vielleicht hält das Betriebssystem es für richtig, dass erst der erste Thread alle Zeit bekommt und danach der zweite bei dir...</p>
<p>Zumal das was du da machst, ja auch noch keine Zeit kostet. 100 Addition ist lächerlich, der Prozessor geht da nebenbei noch shoppen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2402327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402327</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Wed, 04 Jun 2014 09:19:55 GMT</pubDate></item><item><title><![CDATA[Reply to Verhalten von Boost-Threads unter MS VS 2010 on Wed, 04 Jun 2014 09:31:41 GMT]]></title><description><![CDATA[<p>Danke für die Info.</p>
<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>Es ist nicht garantiert in welcher Reihenfolge die Threads Prozessorzeit bekommen (das hast du ja selbst festgestellt). Und vielleicht hält das Betriebssystem es für richtig, dass erst der erste Thread alle Zeit bekommt und danach der zweite bei dir...</p>
<p>Zumal das was du da machst, ja auch noch keine Zeit kostet. 100 Addition ist lächerlich, der Prozessor geht da nebenbei noch shoppen...</p>
</blockquote>
<p>Es ist ja auch nur ein Beispiel und manchmal sieht man die Parallelität der Threads in der Ausgabe. Hatte nur ein deutlicheres Ergebnis erwartet.</p>
<p>Gibt es in Boost bzw. im C++11 Standard ausser die Prioritaet Steuermechanismen die Verteilung zu steuern?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2402333</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402333</guid><dc:creator><![CDATA[WiederMalamRaetseln]]></dc:creator><pubDate>Wed, 04 Jun 2014 09:31:41 GMT</pubDate></item><item><title><![CDATA[Reply to Verhalten von Boost-Threads unter MS VS 2010 on Wed, 04 Jun 2014 10:03:05 GMT]]></title><description><![CDATA[<p>WiederMalamRaetseln schrieb:</p>
<blockquote>
<p>Gibt es in Boost bzw. im C++11 Standard ausser die Prioritaet Steuermechanismen die Verteilung zu steuern?</p>
</blockquote>
<p>.. bedingt. Versuche mal:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;boost\thread.hpp&gt;

void increment( boost::mutex&amp; incrementMutex, int&amp; counter, const int ncount)
{
    for (int i(0); i &lt; ncount; ++i )
    {
        {
        boost::lock_guard&lt;boost::mutex&gt; lock(incrementMutex);
        counter++;
        std::cout &lt;&lt; &quot;+ &quot; &lt;&lt; counter &lt;&lt; std::endl;
        }
        boost::this_thread::yield();
    }
}

void decrement( boost::mutex&amp; incrementMutex, int&amp; counter, const int ncount)
{
    for (int i(0); i &lt; ncount; ++i )
    {
        {
        boost::lock_guard&lt;boost::mutex&gt; lock(incrementMutex);
        counter--;
        std::cout &lt;&lt; &quot;- &quot; &lt;&lt; counter &lt;&lt; std::endl;
        }
        boost::this_thread::yield();
    }
}

int main(int argc, char* argv[])
{
    int counter = 0;
    const int NCOUNT = 100;
    boost::mutex incrementMutex;

    boost::thread a(std::bind(increment, boost::ref(incrementMutex), boost::ref(counter), NCOUNT));
    boost::thread b(std::bind(decrement, boost::ref(incrementMutex), boost::ref(counter), NCOUNT));

    a.join();
    b.join();

    std::cin.get();

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2402344</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402344</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Wed, 04 Jun 2014 10:03:05 GMT</pubDate></item><item><title><![CDATA[Reply to Verhalten von Boost-Threads unter MS VS 2010 on Wed, 04 Jun 2014 10:21:38 GMT]]></title><description><![CDATA[<p>Hallo Werner,</p>
<p>jetzt ist das Verhalten so wie ich es erwartet habe. Der Counter schwankt je nach Programmstart zwichen -20 und 20, meistens aber nur zwischen -10 bis 10.</p>
<p>Mit dem yield wird der Thread explizit freigegeben. Oder?</p>
<p>Warum übergibst du Referenzen statt wie im Beispiel der direkte Zugriff auf die globalen Variablen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2402350</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402350</guid><dc:creator><![CDATA[WiederMalamRaetseln]]></dc:creator><pubDate>Wed, 04 Jun 2014 10:21:38 GMT</pubDate></item><item><title><![CDATA[Reply to Verhalten von Boost-Threads unter MS VS 2010 on Wed, 04 Jun 2014 11:03:20 GMT]]></title><description><![CDATA[<p>zu <code>yield</code> siehe <a href="http://www.boost.org/doc/libs/1_55_0/doc/html/thread/thread_management.html#thread.thread_management.this_thread.yield" rel="nofollow">http://www.boost.org/doc/libs/1_55_0/doc/html/thread/thread_management.html#thread.thread_management.this_thread.yield</a></p>
<blockquote>
<p><strong>Effects:</strong><br />
Gives up the remainder of the current thread's time slice, to allow other threads to run.</p>
</blockquote>
<p>WiederMalamRaetseln schrieb:</p>
<blockquote>
<p>Warum übergibst du Referenzen statt wie im Beispiel der direkte Zugriff auf die globalen Variablen?</p>
</blockquote>
<p>zum einen wollte ich sicher stellen, dass sich mit dem globalen Mutex nichts ändert und zum anderen ..</p>
<p>.. globale Variablen sind: <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_down"
      title=":-1:"
      alt="👎"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2402361</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402361</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Wed, 04 Jun 2014 11:03:20 GMT</pubDate></item></channel></rss>