Boost C++ Libraries: Ticket #12120: Performance improvement in thread/barrier.hpp https://svn.boost.org/trac10/ticket/12120 <p> The current Boost code in thread/barrier.hpp has this code: </p> <hr /> <blockquote> <p> bool wait() { </p> <blockquote> <p> boost::unique_lock &lt; boost::mutex &gt; lock(m_mutex); unsigned int gen = m_generation; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> if (--m_count == 0) { </p> <blockquote> <p> m_generation++; m_count = static_cast&lt;unsigned int&gt;(fct_()); BOOST_ASSERT(m_count != 0); m_cond.notify_all(); return true; </p> </blockquote> <p> } </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> while (gen == m_generation) </p> <blockquote> <p> m_cond.wait(lock); </p> </blockquote> <p> return false; </p> </blockquote> <p> } </p> </blockquote> <hr /> <p> In the line m_cond.notify_all(); m_mutex is locked while the other threads are notified. This is a performance anti-pattern: the notified threads will first have to wait for the mutex to be released. </p> <p> By adding the line: lock.unlock(); before the m_cond.notify_all(); the performance of the barrier class is improved. </p> <p> More info why this is better, see sample in: <a class="ext-link" href="http://en.cppreference.com/w/cpp/thread/condition_variable"><span class="icon">​</span>http://en.cppreference.com/w/cpp/thread/condition_variable</a> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12120 Trac 1.4.3 viboes Mon, 08 Aug 2016 22:51:55 GMT owner, status changed https://svn.boost.org/trac10/ticket/12120#comment:1 https://svn.boost.org/trac10/ticket/12120#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> </ul> Ticket viboes Mon, 08 Aug 2016 23:20:21 GMT milestone changed https://svn.boost.org/trac10/ticket/12120#comment:2 https://svn.boost.org/trac10/ticket/12120#comment:2 <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> Ticket viboes Fri, 19 Aug 2016 21:13:44 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/12120#comment:3 https://svn.boost.org/trac10/ticket/12120#comment:3 <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