<?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[c++ 11 sync(dynamische erzeugung, sync)]]></title><description><![CDATA[<p>Hallo liebe Leute,</p>
<p>Ich erzeuge Threads dynamisch(schleife)</p>
<pre><code>std::vector&lt;std::thread*&gt; threadVector;
for (int i = 0; i &lt; mNumberofPlayers; i++) {

		std::thread *f = new std::thread(Start, i);
		threadVector.push_back(f);
	}

	for (int i = 0; i &lt; mNumberofPlayers; i++) {

		threadVector[i]-&gt;join();
	}
</code></pre>
<p>Und möchte die nun auch dynamisch synchronisieren(2 wartet auf den 1, 3 auf den 2 etc).<br />
SInnhaftigkeit sei hier dann natürlich mal so hingestellt..</p>
<p>edit: dynamisch erstelle future variablen die dann dynamisch nach der reihe abgefragt werden die mit std::launch::dereffed gestartet werden?(find ich net wirklich schön)</p>
<p>Und nochwas:</p>
<p>bei folgendem beispiel werden dynamisch 255 thread aufgemacht auf die ich bis zum cout warten will(zeile 10 und 11)<br />
funktioniert hier antprlich ganz gut, allerdings blockiert der Mutex im Workerthread nicht alle anderen Workethreads?(also es kann immer nur einer ausgeführt werden?!?)</p>
<pre><code>void WorkerThread_2(size_t i){
	std::lock_guard&lt;std::mutex&gt; lck(threadMut2);
        //working...
	dataReady2=true;
		condVar2.notify_one();
}
</code></pre>
<pre><code>for(int i=0; i&lt; 255; i++){

			std::thread *newThread= new std::thread(WorkerThread_2, i);
			myThreadVector2.push_back(newThread);

		}
		//
	//sync, wait for 255 workerthread_2
		for(int i=0;i&lt;255;i++){
			std::unique_lock&lt;std::mutex&gt; lck(threadMut2);
			condVar2.wait(lck, []{return dataReady2;});
		}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/325464/c-11-sync-dynamische-erzeugung-sync</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Jul 2026 13:24:14 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325464.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 03 May 2014 20:18:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sat, 03 May 2014 20:28:19 GMT]]></title><description><![CDATA[<p>Hallo liebe Leute,</p>
<p>Ich erzeuge Threads dynamisch(schleife)</p>
<pre><code>std::vector&lt;std::thread*&gt; threadVector;
for (int i = 0; i &lt; mNumberofPlayers; i++) {

		std::thread *f = new std::thread(Start, i);
		threadVector.push_back(f);
	}

	for (int i = 0; i &lt; mNumberofPlayers; i++) {

		threadVector[i]-&gt;join();
	}
</code></pre>
<p>Und möchte die nun auch dynamisch synchronisieren(2 wartet auf den 1, 3 auf den 2 etc).<br />
SInnhaftigkeit sei hier dann natürlich mal so hingestellt..</p>
<p>edit: dynamisch erstelle future variablen die dann dynamisch nach der reihe abgefragt werden die mit std::launch::dereffed gestartet werden?(find ich net wirklich schön)</p>
<p>Und nochwas:</p>
<p>bei folgendem beispiel werden dynamisch 255 thread aufgemacht auf die ich bis zum cout warten will(zeile 10 und 11)<br />
funktioniert hier antprlich ganz gut, allerdings blockiert der Mutex im Workerthread nicht alle anderen Workethreads?(also es kann immer nur einer ausgeführt werden?!?)</p>
<pre><code>void WorkerThread_2(size_t i){
	std::lock_guard&lt;std::mutex&gt; lck(threadMut2);
        //working...
	dataReady2=true;
		condVar2.notify_one();
}
</code></pre>
<pre><code>for(int i=0; i&lt; 255; i++){

			std::thread *newThread= new std::thread(WorkerThread_2, i);
			myThreadVector2.push_back(newThread);

		}
		//
	//sync, wait for 255 workerthread_2
		for(int i=0;i&lt;255;i++){
			std::unique_lock&lt;std::mutex&gt; lck(threadMut2);
			condVar2.wait(lck, []{return dataReady2;});
		}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397261</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397261</guid><dc:creator><![CDATA[wrock]]></dc:creator><pubDate>Sat, 03 May 2014 20:28:19 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sat, 03 May 2014 20:28:46 GMT]]></title><description><![CDATA[<p>wrock schrieb:</p>
<blockquote>
<p>Ich erzeuge Threads dynamisch(schleife)</p>
<pre><code>std::vector&lt;std::thread*&gt; threadVector;
</code></pre>
</blockquote>
<p>Nein! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /><br />
Nimm std::vector<a href="std::thread" rel="nofollow">std::thread</a> ohne Sinnloszeiger. Für irgendetwas gibts Move-Semantik.</p>
<p>Ansonsten, was willst du genau erreichen? Deine Frage ist unklar...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397264</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397264</guid><dc:creator><![CDATA[raiiler]]></dc:creator><pubDate>Sat, 03 May 2014 20:28:46 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sat, 03 May 2014 20:36:04 GMT]]></title><description><![CDATA[<pre><code>std::vector&lt;std::thread*&gt; threadVector;
for (int i = 0; i &lt; mNumberofPlayers; i++) {

        std::thread *f = new std::thread(Start, i);
        threadVector.push_back(f);
    }
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/27a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--right_arrow"
      title=":arrow_right:"
      alt="➡"
    /></p>
<pre><code>std::vector&lt;std::thread&gt; threadVector;
for (int i = 0; i &lt; mNumberofPlayers; i++)
        threadVector.emplace_back(Start, i);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397265</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 03 May 2014 20:36:04 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sat, 03 May 2014 20:51:55 GMT]]></title><description><![CDATA[<p>raiiler schrieb:</p>
<blockquote>
<p>wrock schrieb:</p>
<blockquote>
<p>Ich erzeuge Threads dynamisch(schleife)</p>
<pre><code>std::vector&lt;std::thread*&gt; threadVector;
</code></pre>
</blockquote>
<p>Nein! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /><br />
Nimm std::vector<a href="std::thread" rel="nofollow">std::thread</a> ohne Sinnloszeiger. Für irgendetwas gibts Move-Semantik.</p>
<p>Ansonsten, was willst du genau erreichen? Deine Frage ist unklar...</p>
</blockquote>
<p>ähm...Zeile 4 das Ganze wird zur Laufzeit erzeugt inwiefern sollte das so unschön sein?</p>
<p>in der schleife werden i Threads erzeugt, Thread 2 soll auf Thread 1 warten bevor er weitermacht, thread 3 auf thread 2...etc. jetzt nicht so schwer...</p>
<p>edit: hab emplace_back nachgelesen...<br />
...soll heißen ich brauch mich ums löschen dann auch nicht mehr kümmern oder? nice <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>schreibt doch dazu, c++11 haben noch nicht alle alles intus...</p>
<p>edit:edit:</p>
<p>Arcoth schrieb:</p>
<blockquote>
<p>std::vector<a href="std::thread" rel="nofollow">std::thread</a> threadVector;<br />
for (int i = 0; i &lt; mNumberofPlayers; i++)<br />
threadVector.emplace_back(<strong>std::thread(<strong>Start, i</strong>)</strong>);</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2397266</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397266</guid><dc:creator><![CDATA[wrock]]></dc:creator><pubDate>Sat, 03 May 2014 20:51:55 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sat, 03 May 2014 20:43:09 GMT]]></title><description><![CDATA[<p>Du solltest std::thread btw nie<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/2122.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--trade_mark"
      title=":trade_mark:"
      alt="™"
    /> raw benutzen btw, sondern immer eine Klasse drum bauen die im Destruktor entweder joined oder detached.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397269</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397269</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sat, 03 May 2014 20:43:09 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sat, 03 May 2014 22:02:51 GMT]]></title><description><![CDATA[<p>wrock schrieb:</p>
<blockquote>
<p>Arcoth schrieb:</p>
<blockquote>
<p>std::vector<a href="std::thread" rel="nofollow">std::thread</a> threadVector;<br />
for (int i = 0; i &lt; mNumberofPlayers; i++)<br />
threadVector.emplace_back(<strong>std::thread(<strong>Start, i</strong>)</strong>);</p>
</blockquote>
</blockquote>
<p>Nein, das war schon richtig von Arcoth, das std::thread davor ist unnötig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397274</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397274</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 03 May 2014 22:02:51 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sat, 03 May 2014 23:14:28 GMT]]></title><description><![CDATA[<p>cooky451 schrieb:</p>
<blockquote>
<p>Du solltest std::thread btw nie<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/2122.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--trade_mark"
      title=":trade_mark:"
      alt="™"
    /> raw benutzen btw, sondern immer eine Klasse drum bauen die im Destruktor entweder joined oder detached.</p>
</blockquote>
<p>Auf keinen Fall! Das führt beides zu Problemen:</p>
<pre><code class="language-cpp">{
  int local_variable; // gedacht um Resultat von t zu halten
  cookies_detaching_thread t([&amp;local_variable]{...});
  throw exception();
  t.join();             // wird nie
  return local_variable; // ausgeführt
} // t1.detach() // -&gt; t1 schreibt den Stack kaputt
</code></pre>
<pre><code class="language-cpp">{
  int local_variable; // gedacht um Resultat von t zu halten
  cookies_joining_thread t([&amp;local_variable]{...});
  throw exception();
  t.join();              // wird nie
  return local_variable; // ausgeführt
} // t.join() // -&gt; Es wird auf t gewartet, aber es macht keinen Sinn t weiter auszuführen
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397277</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397277</guid><dc:creator><![CDATA[antiraii]]></dc:creator><pubDate>Sat, 03 May 2014 23:14:28 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sun, 04 May 2014 00:35:57 GMT]]></title><description><![CDATA[<p>Am besten benutzt du auch keine Schleifen, das kann zu Problemen führen:</p>
<pre><code>for (std::uint16_t i = 0; i &lt; 123456; ++i)
  ; // Kommt nie aus der Schleife raus
</code></pre>
<p>Ah ne moment vielleicht liegt das Problem hier nicht bei der Schleife. Am besten du benutzt einfach keine ints, die sind gefährlich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397279</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397279</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sun, 04 May 2014 00:35:57 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sun, 04 May 2014 05:57:12 GMT]]></title><description><![CDATA[<p>Ich glaube wir sollten auf beiden Seiten von voreiligen Generalisierungen abweichen.</p>
<p>Irgendwo hat cooky Recht, aber antiraii (doofer Name oO) hat natürlich auch schöne Beispiele angegeben, die crashen. Daher, es kommt auf den Anwendungsfall an.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397280</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397280</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Sun, 04 May 2014 05:57:12 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sun, 04 May 2014 08:51:47 GMT]]></title><description><![CDATA[<p>dazu gleich mal ne Frage, beim joinale/detached thread wie muss cih da den construktor aufbauen damit er alle argumente weiterleitet? also das nichts velroren geht?</p>
<pre><code>class joining_thread{

public:
	joining_thread(){}

	~joining_thread(){
		mThread.join();
	}
private:
	std::thread mThread;

};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397287</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397287</guid><dc:creator><![CDATA[wrock]]></dc:creator><pubDate>Sun, 04 May 2014 08:51:47 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sun, 04 May 2014 11:13:25 GMT]]></title><description><![CDATA[<p>So in der Art:</p>
<pre><code>template &lt;typename Function, typename... Args&gt;
	explicit scoped_thread(Function&amp;&amp; f, Args&amp;&amp;... args)
		: thr_(std::forward&lt;Function&gt;(f), std::forward&lt;Args&gt;(args)...)
	{}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397304</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397304</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sun, 04 May 2014 11:13:25 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sun, 04 May 2014 15:02:30 GMT]]></title><description><![CDATA[<p>cooky451 schrieb:</p>
<blockquote>
<p>So in der Art:</p>
<pre><code>template &lt;typename Function, typename... Args&gt;
	explicit scoped_thread(Function&amp;&amp; f, Args&amp;&amp;... args)
		: thr_(std::forward&lt;Function&gt;(f), std::forward&lt;Args&gt;(args)...)
	{}
</code></pre>
</blockquote>
<p>Dank dir !</p>
<p>noch eine Frage</p>
<pre><code>joining_thread t1(&amp;Mikado::Start,this,0);
	joining_thread t2(&amp;Mikado::Start,this,1);
	joining_thread t3(&amp;Mikado::Start,this,2);

std::vector&lt;joining_thread&gt; myVec1;
	myVec1.emplace_back(&amp;Mikado::Start,this,0);
	myVec1.emplace_back(&amp;Mikado::Start,this,1);
	myVec1.emplace_back(&amp;Mikado::Start,this,2);
</code></pre>
<p>Die beiden Varianten müssten sich doch gleich verhalten oder?<br />
Angelegt werden sie beide gleich, und bei empalce werden sie nur in den Vektor verschoben.<br />
Nichtsdestotrotz funktioniertrt die Erste Variante mit meinm Program einwandfrei, und die Zweite nicht....</p>
<p>kann mir eig nur einen Fehler mit der this reference vorstellen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397332</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397332</guid><dc:creator><![CDATA[wrock]]></dc:creator><pubDate>Sun, 04 May 2014 15:02:30 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sun, 04 May 2014 15:04:12 GMT]]></title><description><![CDATA[<blockquote>
<p>Die beiden Varianten müssten sich doch gleich verhalten oder?</p>
</blockquote>
<p>Kommt auf <code>joining_thread</code> an, weil die Elemente des <code>vector</code> s intern wahrscheinlich noch gemoved werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397333</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397333</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sun, 04 May 2014 15:04:12 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sun, 04 May 2014 15:17:29 GMT]]></title><description><![CDATA[<p>naja eigentlich nichts aufregende mehr</p>
<p>die copy konstruktoren sind leer implementiert</p>
<p>und die move semantik so:</p>
<pre><code>//move semantik
	joining_thread(joining_thread&amp;&amp; other ):mThread(std::move(other.mThread)){}
	joining_thread&amp; operator=(joining_thread&amp;&amp; other){
		mThread=std::move(other.mThread);
		return *this;
	}
</code></pre>
<p>demanch sollte es ja richtig verschoben werden oder?</p>
<p>Das Program kompiliert richtig weiter, sieht allerdings so aus als habe ehr bei den Mutex/Threads aufwecken dann ein problem...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397334</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397334</guid><dc:creator><![CDATA[wrock]]></dc:creator><pubDate>Sun, 04 May 2014 15:17:29 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sun, 04 May 2014 15:21:27 GMT]]></title><description><![CDATA[<p>Präzise Fehlerbeschreibungen sind das A und O beim Fragen stellen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397337</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397337</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sun, 04 May 2014 15:21:27 GMT</pubDate></item><item><title><![CDATA[Reply to c++ 11 sync(dynamische erzeugung, sync) on Sun, 04 May 2014 15:33:56 GMT]]></title><description><![CDATA[<pre><code>func1(){//incr counter
if ((mPlayerCounter + 1) == mNumberofPlayers) {
		mPlayerCounter = 0;
	} else {
		mPlayerCounter++;
	}
//wake up all threads
std::unique_lock&lt;std::mutex&gt; lck(mCritMutex);
	mCondVar.notify_all();
}

func2(int PlayerID){
while(true){
  if(mPlayerCounter==PlayerID){
     //do things
     func1();
   }

     //going to sleep, wakes up when change in PlayerCounter
		//lambda functions is used for
     std::unique_lock&lt;std::mutex&gt; lck(mCritMutex);
     mCondVar.wait(lck,[&amp;]{return mPlayerCounter==PlayerID;});
}
}
</code></pre>
<pre><code>std::vector&lt;joining_thread&gt; myVec1;

	/*joining_thread t1(&amp;MyClass::func2,this,0);
	joining_thread t2(&amp;MyClass::func2,this,1);
	joining_thread t3(&amp;MyClass::func2,this,2);
*/

	myVec1.emplace_back(&amp;MyClass::func2,this,0);
	myVec1.emplace_back(&amp;MyClass::func2,this,1);
	myVec1.emplace_back(&amp;MyClass::func2,this,2);
</code></pre>
<p>erste Variante(ausgeklammert) funktioniert&gt;:<br />
thread 0 wird ausgeführt<br />
thread 1 wird ausgeführt<br />
thread 2 wird ausgeführt<br />
thread 0 wird ausgeführt<br />
.<br />
.<br />
.</p>
<p>variante 2:<br />
thread 0 wird ausgeführt<br />
thread 1 wird ausgeführt<br />
--&gt;ende</p>
<p>joining_thread</p>
<pre><code>class joining_thread{

public:

	template &lt;typename Function, typename... Args&gt;
	    explicit joining_thread(Function&amp;&amp; f, Args&amp;&amp;... args)
	        : mThread(std::forward&lt;Function&gt;(f), std::forward&lt;Args&gt;(args)...)
	    {}

	~joining_thread(){
		mThread.join();
	}

	//copy semantik
	joining_thread(joining_thread&amp; other ){}
	joining_thread&amp; operator=(const joining_thread&amp; other ){return *this;}

	//move semantik
	joining_thread(joining_thread&amp;&amp; other ):mThread(std::move(other.mThread)){}
	joining_thread&amp; operator=(joining_thread&amp;&amp; other){
		mThread=std::move(other.mThread);
		return *this;
	}

private:
	std::thread mThread;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397338</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397338</guid><dc:creator><![CDATA[wrock]]></dc:creator><pubDate>Sun, 04 May 2014 15:33:56 GMT</pubDate></item></channel></rss>