ConcurrentJobManager uses ThreadPool to do parallel Gdal RasterIO calls. Some tile gets corrupted...

This commit is contained in:
Erik Broberg
2016-05-20 12:55:37 -04:00
parent bfb8e1a92d
commit bb551432e3
5 changed files with 16 additions and 36 deletions

View File

@@ -65,7 +65,6 @@ namespace openspace {
class ConcurrentJobManager{
public:
ConcurrentJobManager()
: _hasWorkingThread(false)
{
}
@@ -76,17 +75,14 @@ namespace openspace {
void enqueueJob(std::shared_ptr<Job<P>> 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<Job<P>> 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<std::shared_ptr<Job<P>>> _incomingJobs;
ConcurrentQueue<std::shared_ptr<Job<P>>> _finishedJobs;
// Using this atomic bool is probably not optimal - Should probably
// use a conditional variable instead
std::atomic<bool> _hasWorkingThread;
std::atomic<int> _numActiveThreads;
};

View File

@@ -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<std::mutex> lock(queue_mutex);
tasks.clear();
} // release lock
}
} // namespace openspace

View File

@@ -59,6 +59,7 @@ namespace openspace {
~ThreadPool();
void enqueue(std::function<void()> f);
void clearTasks();
private:
friend class Worker;

View File

@@ -45,6 +45,7 @@ namespace openspace {
bool TileProvider::hasInitializedGDAL = false;
ThreadPool TileProvider::threadPool(10);
TileProvider::TileProvider(

View File

@@ -82,7 +82,7 @@ namespace openspace {
void prerender();
static ThreadPool threadPool;
private: