<?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[&#x2F;&#x2F;Brainbug-kl. Kopfnuss - Thread Synchroniserung]]></title><description><![CDATA[<p>Hallo @All<br />
Hab gestern eine alte Klasse rausgekramt und dabei ist mir aufgefallen, das eine mögliche Deadlock Gefahr besteht. Allerdings bin ich mit der Lösung noch nicht 100% zufrieden</p>
<p>Folgende Ausgangslage:</p>
<p>- <strong>receiveData</strong>() wird für N Elemente aufgerufen, deren Daten in einem gemeinsamen Datenpool gespeichert werden. Sobald für das erste Element diese Funktion aufgerufen wird, soll die Aktualisierung der Daten aktivert werden und der aufrufende Prozess blockiert werden bis:</p>
<p>- <strong>updateDataPool</strong>( vec ) den Pool aktualisiert hat. Diese Methode darf aber erst dann ausgeführt werden, wenn dies von receiveData() angefordert wurde. Das Update wird stets von einem anderen Thread durchgeführt.</p>
<p>- <strong>prepareForReceive</strong>() ist eine Hilfsfunktion der Basisklasse, die immer for receiveData aufgerufen wird.</p>
<p>Meine bisherige Lösung:</p>
<pre><code class="language-cpp">struct DataPool{
   DataPool() 
    :   nofElements(10), counterPrepareForReceive(0), counterReceive(0)
      , performPoolUpdate(false), updatedPool 
   {
      //...
   }
   void prepareForReceive()
   {
      counterPrepareForReceive++;
      if( counterPrepareForReceive == nofElements )
      {
         boost::mutex::scoped_lock lock(mutex); 
         counterPrepareForReceive = 0;
         //udpdate ermoeglichen:
         this-&gt;performPoolUpdate  = true; 
         performPoolUpdateCond.notify_one();
      }
   }
   void receiveData()
   {
      if( counterReceive == 0 )
      {
         boost::mutex::scoped_lock lock(newPoolDataMutex); 
         //auf update warten
         while(!this-&gt;poolWasUpdated)
            waitForPoolUpdateCond.wait(lock);
      }
      counterReceive++;
      if( counterReceive == nofElements ) counterReceive = 0;
   }
   void updateDataPool(const Pool&amp; pool)
   {
      boost::mutex::scoped_lock lock(mutex); 
      while(!this-&gt;performPoolUpdate)
         this-&gt;performPoolUpdateCond.wait(lock); 

      /* ... update pool ...*/

      //erfolgreiches update bekannt geben
      this-&gt;poolWasUpdated= true; 
      this-&gt;waitForPoolUpdateCond.notify_one(); 
   }
   int              nofElements;
   int              counterPrepareForReceive;
   int              counterReceive;

   boost::mutex     mutex;
   bool             performPoolUpdate;
   bool             poolWasUpdated;
   boost::condition performPoolUpdateCond;
   boost::condition waitForPoolUpdateCond};
};
//thread1
int main()
{
   for(int i=0; i&lt;nofElements; i++) dataObj-&gt;prepareForReceive();
   for(int i=0; i&lt;nofElements; i++) dataObj-&gt;receiveData();

   for(int i=0; i&lt;nofElements; i++) dataObj-&gt;prepareForReceive();
   for(int i=0; i&lt;nofElements; i++) dataObj-&gt;receiveData();

   //...
}
//thread2
int main()
{
   dataObj-&gt;updateDataPool();
   dataObj-&gt;updateDataPool();
   //...
}
</code></pre>
<p>An den Methoden und an der Aufrufalgorithmik kann ich nichts ändern, die sind von aussen fix vorgegeben. Es stet mir aber frei prepareForReceive() nicht zu verwenden und eine <em>doNothing</em> Methode draus zu machen.</p>
<p>Frage:<br />
Kann ich hier die Snychroisierung evtl. optimieren? Irgendwie sieht das doppelt gemoppelt aus <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>
]]></description><link>https://www.c-plusplus.net/forum/topic/224420/brainbug-kl-kopfnuss-thread-synchroniserung</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 16:46:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/224420.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 09 Oct 2008 09:55:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to &#x2F;&#x2F;Brainbug-kl. Kopfnuss - Thread Synchroniserung on Thu, 09 Oct 2008 09:55:25 GMT]]></title><description><![CDATA[<p>Hallo @All<br />
Hab gestern eine alte Klasse rausgekramt und dabei ist mir aufgefallen, das eine mögliche Deadlock Gefahr besteht. Allerdings bin ich mit der Lösung noch nicht 100% zufrieden</p>
<p>Folgende Ausgangslage:</p>
<p>- <strong>receiveData</strong>() wird für N Elemente aufgerufen, deren Daten in einem gemeinsamen Datenpool gespeichert werden. Sobald für das erste Element diese Funktion aufgerufen wird, soll die Aktualisierung der Daten aktivert werden und der aufrufende Prozess blockiert werden bis:</p>
<p>- <strong>updateDataPool</strong>( vec ) den Pool aktualisiert hat. Diese Methode darf aber erst dann ausgeführt werden, wenn dies von receiveData() angefordert wurde. Das Update wird stets von einem anderen Thread durchgeführt.</p>
<p>- <strong>prepareForReceive</strong>() ist eine Hilfsfunktion der Basisklasse, die immer for receiveData aufgerufen wird.</p>
<p>Meine bisherige Lösung:</p>
<pre><code class="language-cpp">struct DataPool{
   DataPool() 
    :   nofElements(10), counterPrepareForReceive(0), counterReceive(0)
      , performPoolUpdate(false), updatedPool 
   {
      //...
   }
   void prepareForReceive()
   {
      counterPrepareForReceive++;
      if( counterPrepareForReceive == nofElements )
      {
         boost::mutex::scoped_lock lock(mutex); 
         counterPrepareForReceive = 0;
         //udpdate ermoeglichen:
         this-&gt;performPoolUpdate  = true; 
         performPoolUpdateCond.notify_one();
      }
   }
   void receiveData()
   {
      if( counterReceive == 0 )
      {
         boost::mutex::scoped_lock lock(newPoolDataMutex); 
         //auf update warten
         while(!this-&gt;poolWasUpdated)
            waitForPoolUpdateCond.wait(lock);
      }
      counterReceive++;
      if( counterReceive == nofElements ) counterReceive = 0;
   }
   void updateDataPool(const Pool&amp; pool)
   {
      boost::mutex::scoped_lock lock(mutex); 
      while(!this-&gt;performPoolUpdate)
         this-&gt;performPoolUpdateCond.wait(lock); 

      /* ... update pool ...*/

      //erfolgreiches update bekannt geben
      this-&gt;poolWasUpdated= true; 
      this-&gt;waitForPoolUpdateCond.notify_one(); 
   }
   int              nofElements;
   int              counterPrepareForReceive;
   int              counterReceive;

   boost::mutex     mutex;
   bool             performPoolUpdate;
   bool             poolWasUpdated;
   boost::condition performPoolUpdateCond;
   boost::condition waitForPoolUpdateCond};
};
//thread1
int main()
{
   for(int i=0; i&lt;nofElements; i++) dataObj-&gt;prepareForReceive();
   for(int i=0; i&lt;nofElements; i++) dataObj-&gt;receiveData();

   for(int i=0; i&lt;nofElements; i++) dataObj-&gt;prepareForReceive();
   for(int i=0; i&lt;nofElements; i++) dataObj-&gt;receiveData();

   //...
}
//thread2
int main()
{
   dataObj-&gt;updateDataPool();
   dataObj-&gt;updateDataPool();
   //...
}
</code></pre>
<p>An den Methoden und an der Aufrufalgorithmik kann ich nichts ändern, die sind von aussen fix vorgegeben. Es stet mir aber frei prepareForReceive() nicht zu verwenden und eine <em>doNothing</em> Methode draus zu machen.</p>
<p>Frage:<br />
Kann ich hier die Snychroisierung evtl. optimieren? Irgendwie sieht das doppelt gemoppelt aus <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>
]]></description><link>https://www.c-plusplus.net/forum/post/1596139</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1596139</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Thu, 09 Oct 2008 09:55:25 GMT</pubDate></item></channel></rss>