<div dir="ltr"><div>If two threads hold an upgrade lock, and then both of them try to upgrade:</div><div>-The first thread cannot upgrade to a unique lock until the second thread releases its upgrade lock.<br></div><div>-The second thread cannot upgrade to a unique lock until the first thread releases its upgrade lock.</div>
<div>Hence, deadlock.</div><div><br></div><div>There are two ways to do read/write locks with upgradeability. The more common method used by most other libraries is to make the upgrade a non-blocking tryUpgrade() method or similar. The approach used by boost::threads is, IMO, smarter, but more confusing at first.</div>
<div><br></div><div>Here is the key: An upgrade lock is *not* a special type of read lock that can be upgraded. Don&#39;t think of it that way. Instead, think of it as a special type of write lock that can allow simultaneous read locks.</div>
<div><br></div><div>To use boost::thread terminology, multiple upgrade_locks cannot be simultaneously taken on the same mutex; however, while a single upgrade_lock is taken, an arbitrary number of shared_locks can also still be taken on the same mutex.</div>
<div><br></div><div>So:</div><div>unique_lock: No other locks may be taken at the same time.</div><div>upgrade_lock: Other shared_locks may be taken at the same time, but no other upgrade locks.</div><div>shared_lock: Other shared_locks may be taken at the same time.</div>
<div><br></div><div>You need to keep this in mind when coding, and keep your upgrade_lock usage restricted to those places where you *really* need it.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 27, 2014 at 12:09 PM, Elad Moshe <span dir="ltr">&lt;<a href="mailto:eladm26@gmail.com" target="_blank">eladm26@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="">&gt; <span style="font-family:arial,sans-serif;font-size:13px">If both threads tried to upgrade, you would have a deadlock.</span><br style="font-family:arial,sans-serif;font-size:13px">
<div><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div></div><div><font face="arial, sans-serif">But how is it possible? isn&#39;t the upgrade operation atomic?</font></div><div><font face="arial, sans-serif">if the 2 threads try to upgrade, one will succeed and the other one will block until the first thread will unlock. </font></div>

</div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 27, 2014 at 5:39 PM, Steven Watanabe <span dir="ltr">&lt;<a href="mailto:watanabesj@gmail.com" target="_blank">watanabesj@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">AMDG<br>
<div><div><br>
On 03/27/2014 01:21 AM, Elad Moshe wrote:<br>
&gt; Hi,<br>
&gt; this is my first post here so please bare with me :) .<br>
&gt; I recently used upgrade_lock to try access a shared resource from 2<br>
&gt; different threads.<br>
&gt; I noticed that once the first thread acquired its upgrade_lock the second<br>
&gt; thread was blacked<br>
&gt; until the first thread unlock it.<br>
&gt; can someone please explain me the logic behind this behavior?<br>
&gt; isn&#39;t an upgrade lock just a shared_lock that can be upgraded to<br>
&gt; unique_lock?<br>
&gt; why not let both threads acquire it just like shared_lock until, and if,<br>
&gt; they decide to upgrade it to unique_lock?<br>
&gt;<br>
<br>
</div></div>If both threads tried to upgrade, you would have a deadlock.<br>
<br>
In Christ,<br>
Steven Watanabe<br>
<br>
<br>
_______________________________________________<br>
Boost-users mailing list<br>
<a href="mailto:Boost-users@lists.boost.org" target="_blank">Boost-users@lists.boost.org</a><br>
<a href="http://listarchives.boost.org/mailman/listinfo.cgi/boost-users" target="_blank">http://listarchives.boost.org/mailman/listinfo.cgi/boost-users</a><br>
</blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Boost-users mailing list<br>
<a href="mailto:Boost-users@lists.boost.org">Boost-users@lists.boost.org</a><br>
<a href="http://listarchives.boost.org/mailman/listinfo.cgi/boost-users" target="_blank">http://listarchives.boost.org/mailman/listinfo.cgi/boost-users</a><br></blockquote></div><br></div>

