Merge pull request #824 from OGAWAHirofumi/threadpool-cleanup

Don't polling to wait the exit of threads in CThreadPool destructor
This commit is contained in:
Alexey Sokolov
2015-01-25 13:07:11 +00:00
2 changed files with 8 additions and 5 deletions
+3
View File
@@ -307,6 +307,9 @@ private:
// 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;
+5 -5
View File
@@ -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);
}
}
@@ -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) {