title: std::function in combination with thread c++11 fails debug assertion in vector
body: <p>I want to build a helper class that can accept an std::function created via std::bind) so that i can call this class repeaded from another thread:</p>
  
  <p>short example:</p>
  
  <pre><code>void loopme() {
      std::cout &lt;&lt; "yay";
  }
  
  main () {
     LoopThread loop = { std::bind(&amp;loopme) };
     loop.start();
     //wait 1 second
     loop.stop();
     //be happy about output
  }
  </code></pre>
  
  <p>However, when calling stop() my current implementation will raise the following error: <a href="https://i.stack.imgur.com/aR9hP.png" rel="nofollow noreferrer">debug assertion Failed , see Image: i.stack.imgur.com/aR9hP.png</a>.</p>
  
  <p>Does anyone know why the error is thrown ?
  I don't even use vectors in this example.
  When i dont call loopme from within the thread but directly output to std::cout, no error is thrown.</p>
  
  <p>Here the full implementation of my class:</p>
  
  <pre><code>class LoopThread {
  public:
      LoopThread(std::function&lt;void(LoopThread*, uint32_t)&gt; function) : function_{ function }, thread_{ nullptr }, is_running_{ false }, counter_{ 0 } {};
      ~LoopThread();
      void start();
      void stop();
      bool isRunning() { return is_running_; };
  private:
      std::function&lt;void(LoopThread*, uint32_t)&gt; function_;
      std::thread* thread_;
      bool is_running_;
      uint32_t counter_;
      void executeLoop();
  };
  
  LoopThread::~LoopThread() {
      if (isRunning()) {
          stop();
      }
  }
  
  void LoopThread::start() {
      if (is_running_) {
          throw std::runtime_error("Thread is already running");
      }
      if (thread_ != nullptr) {
          throw std::runtime_error("Thread is not stopped yet");
      }
      is_running_ = true;
      thread_ = new std::thread{ &amp;LoopThread::executeLoop, this };
  }
  
  void LoopThread::stop() {
      if (!is_running_) {
          throw std::runtime_error("Thread is already stopped");
      }
      is_running_ = false;
      thread_-&gt;detach();
  }
  
  void LoopThread::executeLoop() {
      while (is_running_) {
          function_(this, counter_);
          ++counter_;
      }
      if (!is_running_) {
          std::cout &lt;&lt; "end";
      }
      //delete thread_;
      //thread_ = nullptr;
  }
  </code></pre>
  
  <p>I used the following Googletest code for testing (however a simple main method containing the code should work):</p>
  
  <pre><code>void testfunction(pft::LoopThread*, uint32_t i) {
      std::cout &lt;&lt; i &lt;&lt; ' ';
  }
  
  TEST(pfFiles, TestLoop)
  {
      pft::LoopThread loop{ std::bind(&amp;testfunction, std::placeholders::_1, std::placeholders::_2) };
      loop.start();
  
      std::this_thread::sleep_for(std::chrono::milliseconds(500));
  
      loop.stop();
      std::this_thread::sleep_for(std::chrono::milliseconds(2500));
      std::cout &lt;&lt; "Why does this fail";
  
  }
  </code></pre>