<?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[Boost: Read- Writelock]]></title><description><![CDATA[<p>Hallo Forum,</p>
<p>ich portiere gerade in einem Programm die Synchronisation von PThread nach Boost::Thread.</p>
<p>Die Read- und Writelocks machen mir Kopfschmerzen. Mit PThread sehen sie so aus:</p>
<pre><code>...
pthread_rwlock_t configReadWriteLock;
...

void Config::configReadLock() {
  pthread_rwlock_rdlock(&amp;configReadWriteLock);
}

void Config::configWriteLock() {
  pthread_rwlock_wrlock(&amp;configReadWriteLock);
}

void Config::configUnlock() {
  pthread_rwlock_unlock(&amp;configReadWriteLock);
}
</code></pre>
<p>Wenn andere Funktionen in der Config etwas schreiben oder lesen wollen, rufen sie die entsprechenden Lockfunktionen auf und später jeweils einen Unlock.</p>
<p>Mit Boost ist das irgendwie nicht so einfach. Man hat sowas:</p>
<pre><code>boost::shared_mutex configReadWriteLock;
boost::shared_lock&lt;boost::shared_mutex&gt; readLock;
boost::unique_lock&lt;boost::shared_mutex&gt; writeLock;
</code></pre>
<p>Aber die Locks in Boost sind für Scopes konzipiert und nicht für den globalen Gebrauch. Wenn ich sie in die Lock-Funktionen weiter oben packe, sterben sie nach dem Funktionsaufruf sofort wieder. Ein Hack wäre zwar, sie global inital anzulegen und sofort zu unlocken, aber man darf sie dann nicht mehrmals &quot;locken&quot;.<br />
Schwierig zu erklären ...</p>
<p>Kann mit jemand trotzdem helfen?</p>
<p>Danke schon mal.</p>
<p>Viele Grüße,<br />
anjohn</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/320928/boost-read-writelock</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 13:14:07 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/320928.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 17 Oct 2013 10:53:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Boost: Read- Writelock on Thu, 17 Oct 2013 10:53:54 GMT]]></title><description><![CDATA[<p>Hallo Forum,</p>
<p>ich portiere gerade in einem Programm die Synchronisation von PThread nach Boost::Thread.</p>
<p>Die Read- und Writelocks machen mir Kopfschmerzen. Mit PThread sehen sie so aus:</p>
<pre><code>...
pthread_rwlock_t configReadWriteLock;
...

void Config::configReadLock() {
  pthread_rwlock_rdlock(&amp;configReadWriteLock);
}

void Config::configWriteLock() {
  pthread_rwlock_wrlock(&amp;configReadWriteLock);
}

void Config::configUnlock() {
  pthread_rwlock_unlock(&amp;configReadWriteLock);
}
</code></pre>
<p>Wenn andere Funktionen in der Config etwas schreiben oder lesen wollen, rufen sie die entsprechenden Lockfunktionen auf und später jeweils einen Unlock.</p>
<p>Mit Boost ist das irgendwie nicht so einfach. Man hat sowas:</p>
<pre><code>boost::shared_mutex configReadWriteLock;
boost::shared_lock&lt;boost::shared_mutex&gt; readLock;
boost::unique_lock&lt;boost::shared_mutex&gt; writeLock;
</code></pre>
<p>Aber die Locks in Boost sind für Scopes konzipiert und nicht für den globalen Gebrauch. Wenn ich sie in die Lock-Funktionen weiter oben packe, sterben sie nach dem Funktionsaufruf sofort wieder. Ein Hack wäre zwar, sie global inital anzulegen und sofort zu unlocken, aber man darf sie dann nicht mehrmals &quot;locken&quot;.<br />
Schwierig zu erklären ...</p>
<p>Kann mit jemand trotzdem helfen?</p>
<p>Danke schon mal.</p>
<p>Viele Grüße,<br />
anjohn</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360875</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360875</guid><dc:creator><![CDATA[anjohn]]></dc:creator><pubDate>Thu, 17 Oct 2013 10:53:54 GMT</pubDate></item><item><title><![CDATA[Reply to Boost: Read- Writelock on Thu, 17 Oct 2013 11:46:29 GMT]]></title><description><![CDATA[<p>Ich glaube du missverstehst das. boost::shared_mutex ist das was vorher dein pthread_rwlock_t war den kann man auch manuell locken und unlocken. boost::shared_lock ist hingegen so etwas wie ein scoped lockguard der dir nur die Arbeit des manuellen lockens abnimmt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360884</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360884</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Thu, 17 Oct 2013 11:46:29 GMT</pubDate></item><item><title><![CDATA[Reply to Boost: Read- Writelock on Thu, 17 Oct 2013 12:19:14 GMT]]></title><description><![CDATA[<p><s>Für ein echtes Read- und Writelock brauchst du aber ohnehin so etwas wie boost::upgrade_lock</s></p>
<p>edit: Diese Aussage war Blödsinn</p>
<p>Schaue dir das mal an:<br />
<a href="http://stackoverflow.com/questions/989795/example-for-boost-shared-mutex-multiple-reads-one-write" rel="nofollow">http://stackoverflow.com/questions/989795/example-for-boost-shared-mutex-multiple-reads-one-write</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360885</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360885</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Thu, 17 Oct 2013 12:19:14 GMT</pubDate></item><item><title><![CDATA[Reply to Boost: Read- Writelock on Thu, 17 Oct 2013 11:59:46 GMT]]></title><description><![CDATA[<p>Sollte eigentlich einfach so funktionieren: Für Config::configReadLock() nimmst du shared_mutex::lock_shared() für Config::configWriteLock() nimmst du shared_mutex::lock() und für Config::configUnlock() Config::unlock()</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360890</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360890</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Thu, 17 Oct 2013 11:59:46 GMT</pubDate></item><item><title><![CDATA[Reply to Boost: Read- Writelock on Thu, 17 Oct 2013 12:02:18 GMT]]></title><description><![CDATA[<p>TNA schrieb:</p>
<blockquote>
<p>Ich glaube du missverstehst das. boost::shared_mutex ist das was vorher dein pthread_rwlock_t war den kann man auch manuell locken und unlocken. boost::shared_lock ist hingegen so etwas wie ein scoped lockguard der dir nur die Arbeit des manuellen lockens abnimmt.</p>
</blockquote>
<p>Ja den Shared Mutex kann ich auch direkt verwenden und lock und unlock auf ihm aufrufen.<br />
Trotzdem muss ich ja irgendwann mal exklusiven Besitz erlangen. Bisher habe ich dafür nur diese Locks gefunden.</p>
<p>TNA schrieb:</p>
<blockquote>
<p>Für ein echtes Read- und Writelock brauchst du aber ohnehin so etwas wie boost::upgrade_lock</p>
<p>Schaue dir das mal an:<br />
<a href="http://stackoverflow.com/questions/989795/example-for-boost-shared-mutex-multiple-reads-one-write" rel="nofollow">http://stackoverflow.com/questions/989795/example-for-boost-shared-mutex-multiple-reads-one-write</a></p>
</blockquote>
<p>Ich versteh den Unterschied zwischen dem Unique_lock und dem Upgrade_Lock nicht so recht. Erkläre es mir bitte, die Doku von Boost ist irgendwie kompliziert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360891</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360891</guid><dc:creator><![CDATA[anjohn]]></dc:creator><pubDate>Thu, 17 Oct 2013 12:02:18 GMT</pubDate></item><item><title><![CDATA[Reply to Boost: Read- Writelock on Thu, 17 Oct 2013 12:10:43 GMT]]></title><description><![CDATA[<p>TNA schrieb:</p>
<blockquote>
<p>Ich glaube du missverstehst das. boost::shared_mutex ist das was vorher dein pthread_rwlock_t war den kann man auch manuell locken und unlocken. boost::shared_lock ist hingegen so etwas wie ein scoped lockguard der dir nur die Arbeit des manuellen lockens abnimmt.</p>
</blockquote>
<p>Ah, ja doch. Ist es vielleicht doch so einfach:</p>
<pre><code>void Config::configReadLock() {
  configReadWriteLock.lock_shared();
}

void Config::configWriteLock() {
  configReadWriteLock.lock();
}

void Config::configReadUnlock() {
  configReadWriteLock.unlock_shared();
}

void Config::configWriteUnlock() {
  configReadWriteLock.unlock();
}
</code></pre>
<p>Funktioniert und hängt bisher nicht. Nur, dass ich nun keinen universellen Unlock mehr habe, sondern jeweils einen für Read und Write im Gegensatz zu vorher.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360893</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360893</guid><dc:creator><![CDATA[anjohn]]></dc:creator><pubDate>Thu, 17 Oct 2013 12:10:43 GMT</pubDate></item><item><title><![CDATA[Reply to Boost: Read- Writelock on Thu, 17 Oct 2013 12:15:07 GMT]]></title><description><![CDATA[<p>Ja, das ist die Lösung die deinem Vorherigen Design am nächsten kommt und sollte funktionieren. Ich kenne mich mit Boost im speziellen auch nicht so besonders aus und musste mich da erst etwas einlesen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360895</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360895</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Thu, 17 Oct 2013 12:15:07 GMT</pubDate></item><item><title><![CDATA[Reply to Boost: Read- Writelock on Thu, 17 Oct 2013 13:38:45 GMT]]></title><description><![CDATA[<p>Ich habe mir das ganze noch mal durchgelesen und durchdacht. Die moderne c++ Variante wäre vermutlich, wenn die Config einfach Funktionen bieten würde, die ein boost::shared_lock<a href="boost::shared%5C_mutex" rel="nofollow">boost::shared\_mutex</a> und ein boost::unique_lock<a href="boost::shared%5C_mutex" rel="nofollow">boost::shared\_mutex</a> auf den internen mutex rausreichen, wenn ich mich nicht irre.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2360902</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2360902</guid><dc:creator><![CDATA[TNA]]></dc:creator><pubDate>Thu, 17 Oct 2013 13:38:45 GMT</pubDate></item></channel></rss>