Try to fix crash on drop()ping JobManager. More debugging when resampler fails.
[dcpomatic.git] / src / lib / job_manager.cc
index 95961c3fcd99bb468c648adadde387fc3150c9f3..056b9f925e1b237946122d804241614bb87f416c 100644 (file)
 
 using std::string;
 using std::list;
+using std::cout;
 using boost::shared_ptr;
 using boost::weak_ptr;
 
 JobManager* JobManager::_instance = 0;
 
 JobManager::JobManager ()
-       : _last_active_jobs (false)
+       : _terminate (false)
+       , _last_active_jobs (false)
+       , _scheduler (new boost::thread (boost::bind (&JobManager::scheduler, this)))
 {
-       boost::thread (boost::bind (&JobManager::scheduler, this));
+       
+}
+
+JobManager::~JobManager ()
+{
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               _terminate = true;
+       }
+
+       if (_scheduler->joinable ()) {
+               _scheduler->join ();
+       }
 }
 
 shared_ptr<Job>
@@ -97,6 +112,10 @@ JobManager::scheduler ()
 
                {
                        boost::mutex::scoped_lock lm (_mutex);
+                       if (_terminate) {
+                               return;
+                       }
+                       
                        for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
 
                                if (!(*i)->finished ()) {
@@ -137,3 +156,10 @@ JobManager::instance ()
 
        return _instance;
 }
+
+void
+JobManager::drop ()
+{
+       delete _instance;
+       _instance = 0;
+}