thread-pool: re-work termination semantics to avoid problems.

We want a pre-spun-up, shared thread-pool that doesn't get its
workers created & joined frequently.

Change-Id: I29081e3a3e3849ca30e63fd080ee3315d99cbe8d
This commit is contained in:
Michael Meeks
2014-10-30 21:58:36 +00:00
parent 62090f65b8
commit 593a44a12d
3 changed files with 11 additions and 8 deletions

View File

@@ -92,7 +92,7 @@ ThreadPool::ThreadPool( sal_Int32 nWorkers ) :
ThreadPool::~ThreadPool() ThreadPool::~ThreadPool()
{ {
waitUntilWorkersDone(); waitAndCleanupWorkers();
} }
struct ThreadPoolStatic : public rtl::StaticWithInit< boost::shared_ptr< ThreadPool >, struct ThreadPoolStatic : public rtl::StaticWithInit< boost::shared_ptr< ThreadPool >,
@@ -109,9 +109,7 @@ ThreadPool& ThreadPool::getSharedOptimalPool()
return *ThreadPoolStatic::get().get(); return *ThreadPoolStatic::get().get();
} }
/// wait until all the workers have completed and void ThreadPool::waitAndCleanupWorkers()
/// terminate all threads
void ThreadPool::waitUntilWorkersDone()
{ {
waitUntilEmpty(); waitUntilEmpty();
@@ -169,7 +167,6 @@ void ThreadPool::waitUntilEmpty()
pTask->doWork(); pTask->doWork();
delete pTask; delete pTask;
} }
mbTerminate = true;
} }
else else
{ {

View File

@@ -39,14 +39,19 @@ public:
ThreadPool( sal_Int32 nWorkers ); ThreadPool( sal_Int32 nWorkers );
virtual ~ThreadPool(); virtual ~ThreadPool();
/// push a new task onto the work queue
void pushTask( ThreadTask *pTask /* takes ownership */ ); void pushTask( ThreadTask *pTask /* takes ownership */ );
/// wait until all queued tasks are completed
void waitUntilEmpty(); void waitUntilEmpty();
void waitUntilWorkersDone();
private: private:
class ThreadWorker; class ThreadWorker;
friend class ThreadWorker; friend class ThreadWorker;
/// wait until all work is completed, then join all threads
void waitAndCleanupWorkers();
ThreadTask *waitForWork( osl::Condition &rNewWork ); ThreadTask *waitForWork( osl::Condition &rNewWork );
ThreadTask *popWork(); ThreadTask *popWork();

View File

@@ -336,8 +336,9 @@ void importSheetFragments( WorkbookFragment& rWorkbookHandler, SheetFragmentVect
// bar updating. // bar updating.
Application::Yield(); Application::Yield();
} }
// join all the threads: aPool.waitUntilEmpty();
aPool.waitUntilWorkersDone();
// threads joined in ThreadPool destructor
} }
else // single threaded iteration else // single threaded iteration
{ {