Subject: [Boost-bugs] [Boost C++ Libraries] #12120: Performance improvement in thread/barrier.hpp
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-04-11 19:50:03
#12120: Performance improvement in thread/barrier.hpp
------------------------------------------+--------------------------
Reporter: Ronald Holthuizen <ronald@â¦> | Owner: anthonyw
Type: Bugs | Status: new
Milestone: To Be Determined | Component: thread
Version: Boost 1.61.0 | Severity: Optimization
Keywords: barrier wait performance |
------------------------------------------+--------------------------
The current Boost code in thread/barrier.hpp has this code:
---------------------
bool wait()
{
boost::unique_lock < boost::mutex > lock(m_mutex);
unsigned int gen = m_generation;
if (--m_count == 0)
{
m_generation++;
m_count = static_cast<unsigned int>(fct_());
BOOST_ASSERT(m_count != 0);
m_cond.notify_all();
return true;
}
while (gen == m_generation)
m_cond.wait(lock);
return false;
}
------------------
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.
By adding the line:
lock.unlock();
before the m_cond.notify_all(); the performance of the barrier class is
improved.
More info why this is better, see sample in:
http://en.cppreference.com/w/cpp/thread/condition_variable
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/12120> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:20 UTC