]> git.tdb.fi Git - r2c2.git/commitdiff
Eliminate a possible race condition in thread termination
authorMikko Rasa <tdb@tdb.fi>
Thu, 5 Feb 2015 14:19:01 +0000 (16:19 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 5 Feb 2015 15:56:37 +0000 (17:56 +0200)
In the unlikely event that the thread doesn't terminate fast enough after
setting the goal pointer, deleting it would send it a kill signal.
Remove this possibility by explicitly jouning the thread.

source/libr2c2/trainrouteplanner.cpp

index 0d0103cb3f4ef274cd1ea4544d2557f87a9fa8c5..9e74eca1c7b1b2c0c18846d586cf86a774c363a1 100644 (file)
@@ -29,7 +29,11 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
 
 TrainRoutePlanner::~TrainRoutePlanner()
 {
-       delete thread;
+       if(thread)
+       {
+               thread->join();
+               delete thread;
+       }
 }
 
 TrainRoutePlanner::Result TrainRoutePlanner::plan()
@@ -55,9 +59,13 @@ TrainRoutePlanner::Result TrainRoutePlanner::check()
 {
        if(result==PENDING && goal)
        {
+               if(thread)
+               {
+                       thread->join();
+                       delete thread;
+                       thread = 0;
+               }
                finalize_plan();
-               delete thread;
-               thread = 0;
        }
 
        return result;