Don't wait for tasks when destroying a threadpool.

This commit is contained in:
Gunnar Beutner 2012-06-18 06:44:26 +02:00
parent 1d8331ecd8
commit 3ebe553401
2 changed files with 22 additions and 10 deletions

View File

@ -11,15 +11,17 @@ ThreadPool::ThreadPool(long numThreads)
ThreadPool::~ThreadPool(void)
{
unique_lock<mutex> lock(m_Lock);
/* wait for all pending tasks */
while (m_Tasks.size() > 0)
m_CV.wait(lock);
{
unique_lock<mutex> lock(m_Lock);
/* notify worker threads to exit */
m_Alive = false;
m_CV.notify_all();
m_Tasks.clear();
/* notify worker threads to exit */
m_Alive = false;
m_CV.notify_all();
}
m_Threads.join_all();
}
void ThreadPool::EnqueueTask(Task task)
@ -29,6 +31,15 @@ void ThreadPool::EnqueueTask(Task task)
m_CV.notify_one();
}
void ThreadPool::WaitForTasks(void)
{
unique_lock<mutex> lock(m_Lock);
/* wait for all pending tasks */
while (m_Tasks.size() > 0)
m_CV.wait(lock);
}
void ThreadPool::WorkerThreadProc(void)
{
while (true) {
@ -38,10 +49,10 @@ void ThreadPool::WorkerThreadProc(void)
unique_lock<mutex> lock(m_Lock);
while (m_Tasks.size() == 0) {
m_CV.wait(lock);
if (!m_Alive)
return;
m_CV.wait(lock);
}
task = m_Tasks.front();

View File

@ -18,6 +18,7 @@ public:
static ThreadPool::Ptr GetDefaultPool(void);
void EnqueueTask(Task task);
void WaitForTasks(void);
private:
mutex m_Lock;