From: mfranz Date: Tue, 7 Mar 2006 10:26:25 +0000 (+0000) Subject: Jean-Yves Lefort: fix "crash" on exit X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ed9e16d00197f5ded4a24751ee7822c0a24b6d15;p=flightgear.git Jean-Yves Lefort: fix "crash" on exit mf: pthread_cancel doesn't seem to work correctly on all supported platforms. It apparently causes SGBlockingQueue::pop() to correctly leave the thread on the cancellation point pthread_wait(), but the SGGuard() destructor isn't called, so the queue mutex remains locked. This triggered an assert() on pthread_join(). This patch uses an empty ICAO request as signal for the thread to terminate itself. --- diff --git a/src/Environment/environment_ctrl.cxx b/src/Environment/environment_ctrl.cxx index f4bf66e88..b6e8db586 100644 --- a/src/Environment/environment_ctrl.cxx +++ b/src/Environment/environment_ctrl.cxx @@ -344,8 +344,7 @@ FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl () FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl () { #if defined(ENABLE_THREADS) - thread->cancel(); - thread->join(); + thread_stop(); #endif // ENABLE_THREADS delete env; @@ -590,8 +589,7 @@ FGMetarEnvironmentCtrl::fetch_data( const string &icao ) #if defined(ENABLE_THREADS) if (_error_count++ >= 3) { SG_LOG( SG_GENERAL, SG_WARN, "Stop fetching data permanently."); - thread->cancel(); - thread->join(); + thread_stop(); } #endif @@ -707,12 +705,21 @@ FGMetarEnvironmentCtrl::update_metar_properties( const FGMetar *m ) #if defined(ENABLE_THREADS) +void +FGMetarEnvironmentCtrl::thread_stop() +{ + request_queue.push( string() ); // ask thread to terminate + thread->join(); +} + void FGMetarEnvironmentCtrl::MetarThread::run() { while ( true ) { string icao = fetcher->request_queue.pop(); + if (icao.empty()) + return; SG_LOG( SG_GENERAL, SG_INFO, "Thread: fetch metar data = " << icao ); FGMetarResult result = fetcher->fetch_data( icao ); fetcher->result_queue.push( result ); diff --git a/src/Environment/environment_ctrl.hxx b/src/Environment/environment_ctrl.hxx index f2266305c..aee0a368b 100644 --- a/src/Environment/environment_ctrl.hxx +++ b/src/Environment/environment_ctrl.hxx @@ -240,7 +240,7 @@ private: */ MetarThread* thread; - + void thread_stop(); #endif // ENABLE_THREADS int _error_count;