title: A multithreading app compiled with MSVC fails at runtime
body: <p>I've implemented a class that cyclically runs a supplied function.</p>
  
  <pre><code>//Timer.h
  #include &lt;chrono&gt;
  #include &lt;mutex&gt;
  #include &lt;thread&gt;
  
  class Timer {
  public:
      Timer(const std::chrono::milliseconds period, const std::function&lt;void()&gt;&amp; handler);
      ~Timer();
      void Start();
      void Stop();
      bool IsRunning() const;
  
  private:
      const std::function&lt;void()&gt;&amp; handler;
      const std::chrono::milliseconds period;
      bool isRunning = false;
      mutable std::recursive_mutex lock;
      int counter = 0;
  
      void DoLoop(int id);
  };
  
  //Timer.cpp
  #include "Timer.h"
  
  Timer::Timer(const std::chrono::milliseconds period, const std::function&lt;void()&gt;&amp; handler) :handler(handler), period(period), lock(){}
  
  Timer::~Timer() {
      Stop();
  }
  
  void Timer::Stop() {
      lock.lock();
      isRunning = false;  
      lock.unlock();
  }
  
  void Timer::Start() {
      lock.lock();
      if (!isRunning) {
          isRunning = true;
          counter++;
          std::thread(&amp;Timer::DoLoop, this, counter).detach();
      }
      lock.unlock();
  }
  
  void Timer::DoLoop(int id) {
      while (true){
          std::this_thread::sleep_for(period);
          lock.lock();
          bool goOn = isRunning &amp;&amp; counter==id;
          if (goOn) std::thread(handler).detach();
          lock.unlock();
  
          if (!goOn)
              break;
      }
  }
  
  bool Timer::IsRunning() const {
      lock.lock();
      bool isRunning = this-&gt;isRunning;
      lock.unlock();
      return isRunning;
  }
  </code></pre>
  
  <p>And here'a s simple program to see if it works:</p>
  
  <pre><code>void Tick(){ cout &lt;&lt; "TICK" &lt;&lt; endl; }
  
  int main() {
      Timer timer(milliseconds(1000), Tick);
      timer.Start();
      cin.get();
  }
  </code></pre>
  
  <p><strong>When I build the app with g++, the program builds and runs without any problems.</strong> However, when I use the Microsoft's compiler (v18) the program compiles as well, but it fails at runtime.</p>
  
  <p>When I use the release configuration I get the following exception from one of the threads:</p>
  
  <blockquote>
    <p>Unhandled exception at 0x000007F8D8E14A30 (msvcr120.dll) in Program.exe: Fatal program exit requested.</p>
  </blockquote>
  
  <p>When I use the debug configuration, a Microsoft Visual C++ Runtime Library error pops up every second:</p>
  
  <blockquote>
    <p>Debug Error!</p>
    
    <p>Program: ...\path\Program.exe</p>
    
    <p>R6010
    - abort() has been called</p>
  </blockquote>
  
  <p>In both configurations:</p>
  
  <ul>
  <li><p>The exception is thrown/the errors start popping up in the second iteration of the timer's loop.</p></li>
  <li><p>The program does not enter the <code>Tick</code> function even once, even though <code>thread(handler)</code> gets invoked.</p></li>
  <li><p>Although the stack traces at the moment of error differ in those two configurations, neither of them contains anything from my code. Both start with <code>ntdll.dll!UserThreadStart()</code>; the debug one ends with <code>msvcr123d.dll!_NMSG_WRITE()</code> and the release one ends with <code>msvcr120.dll!abort()</code>.</p></li>
  </ul>
  
  <p>Why do the problems appear and why only when the app is compiled with MSVC? Is it some kind of MSVC's bug? Or maybe should I change something in the compiler's configuration?</p>