Skip to content

Commit

Permalink
Don't polling to wait the exit of threads in CThreadPool destructor
Browse files Browse the repository at this point in the history
All values are protected by m_mutex. So we don't need the polling to
wait m_num_threads==0 with wakeups, instead simply use CConditionVariable.
  • Loading branch information
OGAWAHirofumi committed Jan 25, 2015
1 parent 7b1133d commit b8dcb5f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 3 additions & 0 deletions include/znc/Threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ class CThreadPool {
// condition variable for reporting finished cancellation
CConditionVariable m_cancellationCond;

// condition variable for waiting running threads == 0
CConditionVariable m_exit_cond;

// when this is true, all threads should exit
bool m_done;

Expand Down
10 changes: 5 additions & 5 deletions src/Threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,12 @@ void CThreadPool::finishJob(CJob *job) const {
}

CThreadPool::~CThreadPool() {
/* Anyone has an idea how this can be done less ugly? */
CMutexLocker guard(m_mutex);
m_done = true;

while (m_num_threads > 0) {
if (m_num_threads > 0) {
m_cond.broadcast();
guard.unlock();
usleep(100);
guard.lock();
m_exit_cond.wait(m_mutex);
}
}

Expand Down Expand Up @@ -134,6 +131,9 @@ void CThreadPool::threadFunc() {
assert(m_num_threads > 0 && m_num_idle > 0);
m_num_threads--;
m_num_idle--;

if (m_num_threads == 0 && m_done)
m_exit_cond.signal();
}

void CThreadPool::addJob(CJob *job) {
Expand Down

0 comments on commit b8dcb5f

Please sign in to comment.