Modify

#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

Attachments (0)

Change History (3)

comment:1 Changed 14 months ago by viboes

Owner: changed from Anthony Williams to viboes
Status: newassigned

comment:2 Changed 14 months ago by viboes

Milestone: To Be DeterminedBoost 1.62.0

comment:3 Changed 13 months ago by viboes

Resolution: fixed
Status: assignedclosed

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain viboes.
The resolution will be deleted.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.