From ee92bee98fa084645b17f65cebe4eabf70e4dee9 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 5 Feb 2015 16:19:01 +0200 Subject: [PATCH] Eliminate a possible race condition in thread termination 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 | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index 0d0103c..9e74eca 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -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; -- 2.43.0