title: std::thread causing application to abort with error R6010
body: <p>I have a class named Task which internally holds a member std::thread. The general idea is to create a thread that stays alive as requests of processing come.</p>
  
  <pre><code>class Task
  {
  public:
      Task();
      ~Task();
  
      void start();
      // some funny stuff here
  protected:
      Task(const Task&amp; ref);
      void main_function();
  
      std::thread m_thread;
      // more funny stuff like queues, mutexes, etc
  }
  </code></pre>
  
  <p>In the function start() I do:</p>
  
  <pre><code>void Task::start()
  {
      m_thread = std::thread(std::bind(&amp;Task::main_function, this));
  }
  </code></pre>
  
  <p>The problem is that this line calls abort() with the runtime error R6010. I've read somewhere that this may be caused by the destructor of m_thread being called without a previous join but, as the thread hasn't started yet, I can't join it.</p>
  
  <p>I'm running this with Visual Studio 2012</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>So I tried a test example and could not replicate the error. Then I replaced my start function in the problematic function as sugested to this:</p>
  
  <pre><code>void Task::start()
  {
      assert(!m_thread.joinable());
      m_thread = std::thread(&amp;Task::main_function,this);
  }
  </code></pre>
  
  <p>But I still get the error R6010.
  The call stack is:</p>
  
  <pre><code>msvcr110d.dll!_NMSG_WRITE(int rterrnum) Line 226    C
  msvcr110d.dll!abort() Line 62   C
  msvcr110d.dll!terminate() Line 97   C++
  msvcp110d.dll!_Call_func(void * _Data) Line 63  C++
  
  msvcr110d.dll!_callthreadstartex() Line 354 C
  msvcr110d.dll!_threadstartex(void * ptd) Line 337   C
  </code></pre>
  
  <p><strong>UPDATE2:</strong>
  Finally could replicate the problem. The code is as follows. Call foo() in the main function.</p>
  
  <pre><code>class Task 
  {
  public:
      Task() : m_exitFlag(false) 
      { 
          std::cout &lt;&lt; "constructor called" &lt;&lt; std::endl;
      }
  
      ~Task()
      {
          m_lock.lock();
          m_exitFlag = true;
          m_condlock.notify_all();
          m_lock.unlock();
  
          if (m_thread.joinable()) m_thread.join();
          std::cout &lt;&lt; "destructor called" &lt;&lt; std::endl;
       }
  
       void start()
       {
           std::cout &lt;&lt; "Task start" &lt;&lt; std::endl;
           assert(!m_thread.joinable());
           m_thread = std::thread(&amp;Task::main_function, this);
       }
  protected:
       void main_function()
       {
           std::cout &lt;&lt; "thread started" &lt;&lt; std::endl;
           while(1)
           {
               m_lock.lock();
               while(m_queue.empty() &amp;&amp; !m_exitFlag)
                   m_condlock.wait(std::unique_lock&lt;std::mutex&gt;(m_lock));
  
               if (m_exitFlag)
               {
                   m_lock.unlock();
                   std::cout &lt;&lt; "thread exiting" &lt;&lt; std::endl;
                   return;
               }
  
               std::function&lt;void()&gt; f;
               if (!m_queue.empty()) f = m_queue.front();
  
               m_lock.unlock;
               if (f != nullptr) f();
           }
       }
       Task(const Task&amp;ref) { }
  
       Task&amp; operator=(const Task&amp; ref) {
           return *this;
       }
  };
  void foo() {
      Task tk;
      tk.start();
  }
  </code></pre>
  
  <p>I guess there's a race condition somewhere here, as it crashes some times and others don't.
  One thread is inside the critical region in ~Task() and the other as the stack in Update1.</p>