title: Meaning of C++/pthread/join error message "what(): Invalid argument"
body: <p>It is a part of a larger application, a simple timer thread, which works correctly before an attempt to finish it:</p>
  
  <pre><code>boost::asio::io_service io_service;
  shared_ptr&lt;thread&gt; loop;
  // it is not initialised here, just shows the idea
  boost::asio::deadline_timer timer(io_service, interval);
  
  void Timer::spin() {
      loop = make_shared&lt;thread&gt;(&amp;Timer::run, this);
  }
  void Timer::unspin() {
      io_service.stop();
      loop.get()-&gt;join();
  }
  void run() {
     timer.async_wait(bind(&amp;Timer::callbackWrapper, this, _1));
     // restart
     io_service.run();    
  }
  </code></pre>
  
  <p>The thread created in <code>spin</code> is in theory terminated and joined in <code>unspin()</code>. In case when the application ends with <code>join()</code> commented out, there in an error message</p>
  
  <pre><code>terminate called without an active exception
  </code></pre>
  
  <p>which, if I "decrypted" it well, means "main thread finished when some other threads are still running" (I do not know the C++ lingo.)</p>
  
  <p>If, in turn, <code>unspin()</code> is called, the following <em>spotty</em> error message may show:</p>
  
  <pre><code>terminate called after throwing an instance of 'std::system_error'
    what():  Invalid argument
  </code></pre>
  
  <p>which is most probably thrown by <code>thread.join()</code>. The problem is, I searched around Stackexchange, but could not explain the other message. I realise that the problem can be outside the small fragment of code (the application is rather large, so I do not include it). But even if it is so, I would just ask for the meaning of the second error message and its <em>possible</em> causes.</p>