Commit 1d905b14 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Fixed possible deadlock in SolverMonitor

parent 83a7e9a7
Loading
Loading
Loading
Loading
+21 −11
Original line number Diff line number Diff line
@@ -21,14 +21,12 @@ namespace Solvers {
class SolverMonitor
{
public:

   SolverMonitor()
      : timeout_milliseconds( 500 ),
        stopped(true),
        started( false ),
        stopped( false ),
        timer( nullptr )
   {};

   ~SolverMonitor() {};
   {}

   virtual void refresh( bool force = false ) = 0;

@@ -44,7 +42,11 @@ class SolverMonitor

   void runMainLoop()
   {
      stopped = false;
      // We need to use both 'started' and 'stopped' to avoid a deadlock
      // when the loop thread runs this method delayed after the
      // SolverMonitorThread's destructor has already called stopMainLoop()
      // from the main thread.
      started = true;

      const int timeout_base = 100;
      const std::chrono::milliseconds timeout( timeout_base );
@@ -62,6 +64,10 @@ class SolverMonitor
            std::this_thread::sleep_for( timeout );
         }
      }

      // reset to initial state
      started = false;
      stopped = false;
   }

   void stopMainLoop()
@@ -69,8 +75,12 @@ class SolverMonitor
      stopped = true;
   }

   protected:
   bool isStopped() const
   {
      return stopped;
   }

protected:
   double getElapsedTime()
   {
      if( ! timer )
@@ -80,6 +90,7 @@ class SolverMonitor

   std::atomic_int timeout_milliseconds;

   std::atomic_bool started;
   std::atomic_bool stopped;

   Timer* timer;
@@ -111,4 +122,3 @@ class SolverMonitorThread

} // namespace Solvers
} // namespace TNL