Hi,

The attached patch fixes a crash which occurs on exit.

In the FGMetarEnvironmentCtrl destructor, thread->cancel() causes the
following thread->join() call to return without actually waiting on
the thread (btw, thread->cancel() does not cause the thread to exit).

Then the request_queue destructor is automatically invoked, but the
metar thread is still waiting on fetcher->request_queue.pop(), with
the SGBlockingQueue lock held. Destroying the locked mutex causes the
crash:

Assertion failed: (status == 0), function ~SGMutex, file 
/home/jylefort/test/simgear-head/include/simgear/threads/SGThread.hxx, line 227.
Abort trap (core dumped)

-- 
Jean-Yves Lefort

[EMAIL PROTECTED]
http://lefort.be.eu.org/
Index: src/Environment/environment_ctrl.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Environment/environment_ctrl.cxx,v
retrieving revision 1.45
diff -u -r1.45 environment_ctrl.cxx
--- src/Environment/environment_ctrl.cxx        21 Feb 2006 13:19:33 -0000      
1.45
+++ src/Environment/environment_ctrl.cxx        5 Mar 2006 01:30:02 -0000
@@ -344,8 +344,7 @@
 FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
 {
 #if defined(ENABLE_THREADS)
-   thread->cancel();
-   thread->join();
+   stop();
 #endif // ENABLE_THREADS
 
    delete env;
@@ -590,8 +589,7 @@
 #if defined(ENABLE_THREADS)
         if (_error_count++ >= 3) {
            SG_LOG( SG_GENERAL, SG_WARN, "Stop fetching data permanently.");
-           thread->cancel();
-           thread->join();
+          stop();
         }
 #endif
 
@@ -708,11 +706,20 @@
 
 #if defined(ENABLE_THREADS)
 void
+FGMetarEnvironmentCtrl::stop()
+{
+    request_queue.push( string() );    // ask metar 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 );
Index: src/Environment/environment_ctrl.hxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Environment/environment_ctrl.hxx,v
retrieving revision 1.22
diff -u -r1.22 environment_ctrl.hxx
--- src/Environment/environment_ctrl.hxx        21 Feb 2006 13:19:33 -0000      
1.22
+++ src/Environment/environment_ctrl.hxx        5 Mar 2006 01:30:02 -0000
@@ -240,7 +240,7 @@
      */
     MetarThread* thread;
 
-
+    void stop();
 #endif // ENABLE_THREADS
 
     int _error_count;

Attachment: pgpQ2Dq1MeXHm.pgp
Description: PGP signature

Reply via email to