Boost C++ Libraries: Ticket #12350: shared_mutex (pthreads) unlocked too early in unlock_shared() https://svn.boost.org/trac10/ticket/12350 <p> Hej, I experienced some odd behaviour in the unlock_shared() function of the shared_mutex in the pthreads implementation. I wanted to do the following: There is a class node that has a shared_mutex with which I implemented optimistic locking for a tree data structure. A node can be deleted if its parent and the node itself is locked (exclusive) by the current thread. The problem with that boils down to the following order of two threads (R - reader, W - writer) operating on a node in this way: </p> <ol><li>R successfully calls lock_shared() on a node </li><li>W (which holds a shared lock) wants to change the node </li></ol><ul><li>try_unlock_shared_and_lock_upgrade() </li><li>unlock_upgrade_and_lock() puts W to sleep as R still holds a shared lock </li></ul><ol start="3"><li>R calls unlock_shared() and internally does (thread/pthreads/shared_mutex.hpp void unlock_shared()): </li></ol><ul><li>it gets the unique_lock to modify the state </li><li>it decreases the number of shared locks to 0 </li><li>it therefore notifies R and unlocks the unique_lock </li></ul><ol start="4"><li>W modifies the node and deletes it </li><li>R crashes in release_waiters as it now accesses deleted memory </li></ol><p> I fixed this by removing the two premature unlocks (lk.unlock()) but I am not sure if that is valid for all applications. That means that state changes are ownly possible after all operations are actually done (when the unique_lock goes out of scope). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12350 Trac 1.4.3 Maximilian Czerny <maximilian.czerny@…> Mon, 25 Jul 2016 16:37:35 GMT summary changed https://svn.boost.org/trac10/ticket/12350#comment:1 https://svn.boost.org/trac10/ticket/12350#comment:1 <ul> <li><strong>summary</strong> <span class="trac-field-old">shared_mutex (pthreads) unlocked to early in unlock_shared()</span> → <span class="trac-field-new">shared_mutex (pthreads) unlocked too early in unlock_shared()</span> </li> </ul> Ticket viboes Mon, 08 Aug 2016 23:11:27 GMT <link>https://svn.boost.org/trac10/ticket/12350#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12350#comment:2</guid> <description> <p> Sorry for been late. I will take a look at asap. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 09 Aug 2016 00:24:10 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/12350#comment:3 https://svn.boost.org/trac10/ticket/12350#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket viboes Mon, 15 Aug 2016 17:48:44 GMT <link>https://svn.boost.org/trac10/ticket/12350#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12350#comment:4</guid> <description> <p> Good catch. </p> <p> I'll commit the change soon. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Mon, 15 Aug 2016 20:51:39 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/12350#comment:5 https://svn.boost.org/trac10/ticket/12350#comment:5 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.62.0</span> </li> </ul> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/e34d343c12e7b9d53309a1866a409a7284f88460"><span class="icon">​</span>https://github.com/boostorg/thread/commit/e34d343c12e7b9d53309a1866a409a7284f88460</a> </p> Ticket viboes Fri, 19 Aug 2016 21:14:03 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/12350#comment:6 https://svn.boost.org/trac10/ticket/12350#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket