Opened 7 years ago

Closed 6 years ago

#12120 closed Bugs (fixed)

Performance improvement in thread/barrier.hpp

Reported by: Ronald Holthuizen <ronald@…> Owned by: viboes
Milestone: Boost 1.62.0 Component: thread
Version: Boost 1.61.0 Severity: Optimization
Keywords: barrier wait performance Cc:

Description

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

Change History (3)

comment:1 by viboes, 6 years ago

Owner: changed from Anthony Williams to viboes
Status: newassigned

comment:2 by viboes, 6 years ago

Milestone: To Be DeterminedBoost 1.62.0

comment:3 by viboes, 6 years ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.