From bb551432e3ffd39cae5cb4e342eabd79ee38296f Mon Sep 17 00:00:00 2001 From: Erik Broberg Date: Fri, 20 May 2016 12:55:37 -0400 Subject: [PATCH] ConcurrentJobManager uses ThreadPool to do parallel Gdal RasterIO calls. Some tile gets corrupted... --- .../other/concurrentjobmanager.h | 39 +++---------------- modules/globebrowsing/other/threadpool.cpp | 9 ++++- modules/globebrowsing/other/threadpool.h | 1 + modules/globebrowsing/other/tileprovider.cpp | 1 + modules/globebrowsing/other/tileprovider.h | 2 +- 5 files changed, 16 insertions(+), 36 deletions(-) diff --git a/modules/globebrowsing/other/concurrentjobmanager.h b/modules/globebrowsing/other/concurrentjobmanager.h index 33cd150b5a..04df7c5f90 100644 --- a/modules/globebrowsing/other/concurrentjobmanager.h +++ b/modules/globebrowsing/other/concurrentjobmanager.h @@ -65,7 +65,6 @@ namespace openspace { class ConcurrentJobManager{ public: ConcurrentJobManager() - : _hasWorkingThread(false) { } @@ -76,17 +75,14 @@ namespace openspace { void enqueueJob(std::shared_ptr> job) { - _incomingJobs.push(job); - if (!_hasWorkingThread) { - _hasWorkingThread = true; // Can only be set to true by the main thread - executeJobsInSeparateThread(); - } + TileProvider::threadPool.enqueue([this, job]() { + job->execute(); + _finishedJobs.push(job); + }); } void clearEnqueuedJobs() { - while (_incomingJobs.size()) { - _incomingJobs.pop(); - } + TileProvider::threadPool.clearTasks(); } std::shared_ptr> popFinishedJob() { @@ -102,33 +98,8 @@ namespace openspace { private: - - void executeJobsInSeparateThread() { - // Create new thread and run workerThreadMainTask on that thread - std::thread t(&ConcurrentJobManager::workerThreadMainTask, this); - t.detach(); - } - - void workerThreadMainTask() { - while (_incomingJobs.size() > 0) { - auto job = _incomingJobs.pop(); - - job->execute(); - - _finishedJobs.push(job); - } - - _hasWorkingThread = false; // Can only be set to false by worker thread - } - - ConcurrentQueue>> _incomingJobs; ConcurrentQueue>> _finishedJobs; - // Using this atomic bool is probably not optimal - Should probably - // use a conditional variable instead - std::atomic _hasWorkingThread; - std::atomic _numActiveThreads; - }; diff --git a/modules/globebrowsing/other/threadpool.cpp b/modules/globebrowsing/other/threadpool.cpp index 8712ed65b6..daf3c80559 100644 --- a/modules/globebrowsing/other/threadpool.cpp +++ b/modules/globebrowsing/other/threadpool.cpp @@ -113,8 +113,15 @@ namespace openspace { } // release lock // wake up one thread - std::cout << "Notify one thread" << std::endl; condition.notify_one(); } + void ThreadPool::clearTasks() { + { // acquire lock + std::unique_lock lock(queue_mutex); + tasks.clear(); + } // release lock + } + + } // namespace openspace \ No newline at end of file diff --git a/modules/globebrowsing/other/threadpool.h b/modules/globebrowsing/other/threadpool.h index 73ae6a2d6d..d792ee01d0 100644 --- a/modules/globebrowsing/other/threadpool.h +++ b/modules/globebrowsing/other/threadpool.h @@ -59,6 +59,7 @@ namespace openspace { ~ThreadPool(); void enqueue(std::function f); + void clearTasks(); private: friend class Worker; diff --git a/modules/globebrowsing/other/tileprovider.cpp b/modules/globebrowsing/other/tileprovider.cpp index 6ff9d420fa..36c7c85378 100644 --- a/modules/globebrowsing/other/tileprovider.cpp +++ b/modules/globebrowsing/other/tileprovider.cpp @@ -45,6 +45,7 @@ namespace openspace { bool TileProvider::hasInitializedGDAL = false; + ThreadPool TileProvider::threadPool(10); TileProvider::TileProvider( diff --git a/modules/globebrowsing/other/tileprovider.h b/modules/globebrowsing/other/tileprovider.h index 02b70c585b..28b43f44f9 100644 --- a/modules/globebrowsing/other/tileprovider.h +++ b/modules/globebrowsing/other/tileprovider.h @@ -82,7 +82,7 @@ namespace openspace { void prerender(); - + static ThreadPool threadPool; private: