title: Implementing Multi threading with Polymorphic Classes
body: <p>I am trying to implement a Logic system for my game which takes <code>dt</code> as its parameter
  So far I have done this</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;chrono&gt;
  #include &lt;memory&gt;
  
  class Player {
  private:
      float x, y;
  public:
       Player(float initX, float initY)
           : x(initX), y(initY) {}
  
       void Logic(float dt) {
           //Do some Logic
       }
  }
  
  bool isLogicThreadActive = true;
  class GameState {
      public:
      virtual void Logic(float) = 0;
  }
  
  class PlayState : public GameState {
      private:
         Player player;
      public:
         PlayState() {
             player = Player(0.0f, 0.0f); 
         }
  
      void Logic(float dt) override {
          while(isLogicThreadActive) {
              player.Logic(dt);
          }
      }
  }
  
  class Game {
  private:
      std::unique_ptr&lt;GameState&gt; gameState;
      float dt;
  public:
      Game() {
          gameState = std::make_unique&lt;PlayState&gt;();
          dt = 0.0f;
      }
  
      void Logic() {
  
         std::thread logicThread([&amp;]() { gameState-&gt;Logic(dt); });
  
         auto t1 = std::chrono::system_clock::now();
         while(isLogicThreadActive) {
             auto t2 = std::chrono::system_clock::now();
             dt = (float)std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(t2 - t1).count();
             t1 = t2;
         }
  
         if (logicThread.joinable()) {
             logicThread.join();
         }
      }
  }
  </code></pre>
  
  <p>The problem is that the value of dt is not being updated in the Logic Thread.
  Due to this the player is not moving when provided with Input from Main Thread.</p>
  
  <p><strong>However when I tried using std::cout in PlayState::Logic(float dt) method, it started to work.
  Can you explain this and also a solution to how to update the dt variable in thread without affecting FPS?</strong></p>