Boost C++ Libraries: Ticket #11798: Implementation of boost::shared_mutex on POSIX is suboptimal https://svn.boost.org/trac10/ticket/11798 <p> The current (as of boost 1.59) implementation of boost::shared_mutex for 'pthread' is pretty suboptimal as it's using a heavyweight mutex to guard the internal mutex state. This is more evident when shared locks are used to guard state whose access concurrency is high, due to contention on the mutex state lock (in these cases, the shared mutex is effectively exclusive). In comparison, the 'win32' implementation uses lightweight spinlocks underneath. </p> <p> There are a couple options to fix this for 'pthread', e.g. using a spinlock or leveraging pthreads_rwlock. I'm happy to provide with an initial patch for this. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11798 Trac 1.4.3 viboes Mon, 16 Nov 2015 22:22:34 GMT owner, status, type, severity changed https://svn.boost.org/trac10/ticket/11798#comment:1 https://svn.boost.org/trac10/ticket/11798#comment:1 <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> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Tasks</span> </li> <li><strong>severity</strong> <span class="trac-field-old">Problem</span> → <span class="trac-field-new">Optimization</span> </li> </ul> <p> Hi, </p> <p> any patch that makes the code more efficient is welcome. Boost.Thread is an old library that doesn't make use of spinlocks. </p> <p> Ideally, I will prefer a patch that allows to switch to the new optimized version usedg a compiler flag. The reason is that we have a lot of platforms and we need to verify the new code before removing completely the old one. </p> <p> What do you mean by an initial patch? </p> <p> Ah, BTW, I've moved this to a task/optimization ticket, as it is not a bug/problem. </p> Ticket alex@… Mon, 16 Nov 2015 23:46:18 GMT <link>https://svn.boost.org/trac10/ticket/11798#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:2</guid> <description> <p> Sounds good, I'll push a patch. I meant "initial" as I've not committed patches against boost before, so I'll be learning the process as I go :) </p> <p> The code in the patch will be guarded by compile-time flags. </p> </description> <category>Ticket</category> </item> <item> <author>alex@…</author> <pubDate>Tue, 17 Nov 2015 08:09:37 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/11798 https://svn.boost.org/trac10/ticket/11798 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">patch.diff</span> </li> </ul> <p> initial patch for pthread_rwlock* implementation </p> Ticket alex@… Tue, 17 Nov 2015 08:10:21 GMT <link>https://svn.boost.org/trac10/ticket/11798#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:3</guid> <description> <p> A very initial patch is below. Notably, it's lacking a couple things that will prevent this to be production-ready: </p> <ol><li>Doesn't implement the timeout-based locking public methods. This should be relatively straightforward using pthread_rwlock_timedrdlock(3) and pthread_rwlock_timedwrlock(3). </li><li>Doesn't implement any of the upgrade methods. This will be somewhat more convoluted, but perhaps we can leverage the existing boost::mutex to implement those upgrades atomically. </li><li>I'm unclear how this would interact with thread interruption. Have not looked into this enough to have insights at this moment. </li></ol><p> Also, as for code style wrt per-platform ifdefs, exceptions (this patch throws if the pthread_* methods fail, which is unexpected), etc, I'm open to pointers so that I can polish the patch. </p> </description> <category>Ticket</category> </item> <item> <author>alex@…</author> <pubDate>Tue, 17 Nov 2015 09:43:37 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/11798 https://svn.boost.org/trac10/ticket/11798 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">patch.2.diff</span> </li> </ul> <p> updated patch </p> Ticket alex@… Tue, 17 Nov 2015 09:46:17 GMT <link>https://svn.boost.org/trac10/ticket/11798#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:4</guid> <description> <p> Updated the patch with a minor fix. </p> <p> Also note that as mentioned in the original description, we can alternatively keep most of the existing implementation the same and use a pthread_spin_lock instead of a boost::mutex to guard the internal state. This will make the implementation quite similar to the win32 one. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 17 Nov 2015 22:19:40 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11798#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:5</guid> <description> <p> Hi, </p> <p> if you prefer, you can create your own git fork and propose a PR on github. This will allow me to check it without needing to do any merge. </p> <p> Hmm, I missed that you talked of pthread_rwlock_xxx. I believe that this direction will be incompatible with the whole set of function (interruptions, upgrade, downgrade, ...), but who knows. </p> </description> <category>Ticket</category> </item> <item> <author>alex@…</author> <pubDate>Tue, 17 Nov 2015 22:55:18 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11798#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:6</guid> <description> <p> Was unaware that boost has moved to github - a pleasant surprise :) </p> <p> WRT the pthread_rwlock_xxx interface - I'm also starting to see that the alternate route (i.e. having a lightweight spin lock instead of a mutex, similar to the win32 implementation) might be more viable. I'll have a PR on github with this approach soon. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Mon, 11 Jan 2016 18:51:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11798#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:7</guid> <description> <p> Hi, please, don't provide a patch with a lightweight spin lock for this class. I believe that it would merit another name. BTW, there are already spin lock in Boost (I don't remember if in detail or in smart_ptr). </p> </description> <category>Ticket</category> </item> <item> <author>Max <silverhammermba@…></author> <pubDate>Wed, 18 May 2016 17:16:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11798#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:8</guid> <description> <p> I think I just ran into this bug with this repo: <a class="ext-link" href="https://github.com/silverhammermba/sanity"><span class="icon">​</span>https://github.com/silverhammermba/sanity</a> </p> <p> Basically I'm timing how long it takes a bunch of threads to do concurrent reads on a std::unordered_map. In one case the reads are protected by std::lock_guard&lt;std::mutex&gt;, so each thread has to wait its turn. In the other case, the reads are protected by boost::shared_lock&lt;boost::shared_mutex&gt;, so other than checking in with the shared_mutex the reads should be concurrent. </p> <p> One would think this is the ideal use-case for a shared lock: lots of reads, no writes. But on my Arch Linux box the shared_lock version is <strong>almost twice as slow</strong> as a naive mutex. My friend compiled the same code on Windows and saw the exact opposite, with shared_lock being almost twice as fast (as we would hope). </p> <p> This is not just a matter of optimization, <strong>this makes boost::shared_mutex completely useless on Linux</strong>. Please upgrade the severity of this report. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Mon, 08 Aug 2016 23:27:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11798#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:9</guid> <description> <p> Sorry for been late. I will take a look at asap. </p> </description> <category>Ticket</category> </item> <item> <author>Doron Atuar <Dorona@…></author> <pubDate>Mon, 29 Aug 2016 14:56:59 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11798#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:10</guid> <description> <p> Joining this ticket </p> <p> I'll be very happy if it will be fixed soon </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 31 Aug 2016 16:43:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11798#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:11</guid> <description> <p> Hi, I'm the maintainer of Boost.Thread, but I didn't wrote the code for shared mutex. </p> <p> Any help on this issue will be recognized by the whole Boost community. </p> <p> I've no time to work on this issue now. </p> <p> Please, fork the git repository and prepare a PR. </p> </description> <category>Ticket</category> </item> <item> <author>alex@…</author> <pubDate>Thu, 01 Sep 2016 06:54:55 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11798#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:12</guid> <description> <p> I can take a stab at this. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 21 Feb 2017 08:50:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11798#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:13</guid> <description> <p> Any progress? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Mon, 06 Mar 2017 17:35:50 GMT</pubDate> <title>cc set https://svn.boost.org/trac10/ticket/11798#comment:14 https://svn.boost.org/trac10/ticket/11798#comment:14 <ul> <li><strong>cc</strong> <span class="trac-author">Andrey.Semashev@…</span> added </li> </ul> Ticket Nilanjan Wed, 13 Dec 2017 02:08:35 GMT <link>https://svn.boost.org/trac10/ticket/11798#comment:15 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11798#comment:15</guid> <description> <p> Hi, any progress on this? </p> </description> <category>Ticket</category> </item> </channel> </rss>