title: Crash : terminate called after throwing an instance of 'std::system_error' what(): Resource deadlock avoided
body: <p>I have a simple client /server application the code of which is mentioned below. 
  Please run the server in one shell and the client in another shell in linux. 
  First start the server and then the client.
  When the server is done with it's work, it crashes with following exception:</p>
  
  <p><em>terminate called after throwing an instance of 'std::system_error'
    what():  Resource deadlock avoided</em></p>
  
  <p>This happens from the line m_thread->join() from inside the function Service::HandleClient
  I have no clue on what's going on.. Can someone please check the code.. I just want that the server application  should also get closed correctly the way the client application got closed.</p>
  
  <p>**Server Code : **</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  
  #include &lt;thread&gt;
  #include &lt;atomic&gt;
  #include &lt;memory&gt;
  #include &lt;iostream&gt;
  
  using namespace boost;
  
  class Service {
  public:
      Service(){}
  
      void StartHandligClient(
          std::shared_ptr&lt;asio::ip::tcp::socket&gt; sock) {
  
          m_thread.reset(new std::thread (([this, sock]() {
              HandleClient(sock);
          })) );
  
      }
  
  private:
      void HandleClient(std::shared_ptr&lt;asio::ip::tcp::socket&gt; sock) {
          while(1)
          {
              try {
                  asio::streambuf request;
                  std::cout &lt;&lt; "Waiting to read \n";
                  asio::read_until(*sock.get(), request, '\n');
  
                  std::string s( (std::istreambuf_iterator&lt;char&gt;(&amp;request)), std::istreambuf_iterator&lt;char&gt;() );
                  std::cout &lt;&lt; "Server got : " &lt;&lt; s &lt;&lt; "\n";
  
                  // Emulate request processing.
                  int i = 0;
                  while (i != 1000000)
                      i++;
  
                  std::this_thread::sleep_for(
                          std::chrono::milliseconds(500));
  
                  // Sending response.
                  std::string response = "Response\n";
                  asio::write(*sock.get(), asio::buffer(response));
              }
              catch (system::system_error &amp;e) {
                  boost::system::error_code ec = e.code();
                  if(ec == asio::error::eof)
                  {
                      std::cout &lt;&lt; "Breaking loop \n";
                      break;
                  }
                  std::cout &lt;&lt; "Error occured! Error code = "
                      &lt;&lt; e.code() &lt;&lt; ". Message: "
                      &lt;&lt; e.what();
              }
          }
  
          m_thread-&gt;join();
  
          // Clean-up.
          delete this;
      }
      std::unique_ptr&lt;std::thread&gt; m_thread;
  };
  
  class Acceptor {
  public:
      Acceptor(asio::io_service&amp; ios, unsigned short port_num) :
          m_ios(ios),
          m_acceptor(m_ios,
          asio::ip::tcp::endpoint(
          asio::ip::address_v4::any(),
          port_num))
      {
          m_acceptor.listen();
      }
  
      void Accept() {
          std::cout &lt;&lt; "Server Accept() \n" &lt;&lt; std::flush;
          std::shared_ptr&lt;asio::ip::tcp::socket&gt;
              sock(new asio::ip::tcp::socket(m_ios));
  
          std::cout &lt;&lt; "BEFORE calling acceptor's accept function \n" &lt;&lt; std::flush;
          m_acceptor.accept(*sock.get());
          std::cout &lt;&lt; "AFTER calling acceptor's accept function \n" &lt;&lt; std::flush;
  
  
          (new Service)-&gt;StartHandligClient(sock);
      }
  
      void close()
      {
          std::cout &lt;&lt; "Inside Acceptor.close() \n" &lt;&lt; std::flush;
          m_acceptor.close();
      }
  
  private:
      asio::io_service&amp; m_ios;
      asio::ip::tcp::acceptor m_acceptor;
  };
  
  class Server {
  public:
      Server() : m_stop(false) {}
  
      void Start(unsigned short port_num) {
          m_thread.reset(new std::thread([this, port_num]() {
              Run(port_num);
          }));
      }
  
      void Stop() {
          m_stop.store(true);
          m_thread-&gt;join();
      }
  
  private:
      void Run(unsigned short port_num) {
          Acceptor acc(m_ios, port_num);
  
          while (!m_stop.load()) {
              std::cout &lt;&lt; "Server accept\n" &lt;&lt; std::flush;
              acc.Accept();
          }
          acc.close();
      }
  
      std::unique_ptr&lt;std::thread&gt; m_thread;
      std::atomic&lt;bool&gt; m_stop;
      asio::io_service m_ios;
  };
  
  int main()
  {
      unsigned short port_num = 3333;
  
      try {
          Server srv;
          srv.Start(port_num);
  
          std::this_thread::sleep_for(std::chrono::seconds(4));
  
          srv.Stop();
      }
      catch (system::system_error &amp;e) {
          std::cout &lt;&lt; "Error occured! Error code = "
              &lt;&lt; e.code() &lt;&lt; ". Message: "
              &lt;&lt; e.what();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>**Client Code : **</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;iostream&gt;
  
  using namespace boost;
  
  class SyncTCPClient {
  public:
      SyncTCPClient(const std::string&amp; raw_ip_address,
          unsigned short port_num) :
          m_ep(asio::ip::address::from_string(raw_ip_address),
          port_num),
          m_sock(m_ios) {
  
          m_sock.open(m_ep.protocol());
      }
  
      void connect() {
          m_sock.connect(m_ep);
      }
  
      void close() {
          m_sock.shutdown(
              boost::asio::ip::tcp::socket::shutdown_both);
          m_sock.close();
      }
  
      std::string emulateLongComputationOp(
          unsigned int duration_sec) {
  
          std::string request = "EMULATE_LONG_COMP_OP "
              + std::to_string(duration_sec)
              + "\n";
  
          sendRequest(request);
          return receiveResponse();
      };
  
  private:
      void sendRequest(const std::string&amp; request)
      {
          std::cout &lt;&lt; "Inside sendRequest : " &lt;&lt; request &lt;&lt; "\n";
          asio::write(m_sock, asio::buffer(request));
      }
  
      std::string receiveResponse() {
          asio::streambuf buf;
          asio::read_until(m_sock, buf, '\n');
  
          std::istream input(&amp;buf);
  
          std::string response;
          std::getline(input, response);
  
          return response;
      }
  
  private:
      asio::io_service m_ios;
  
      asio::ip::tcp::endpoint m_ep;
      asio::ip::tcp::socket m_sock;
  };
  
  int main()
  {
      const std::string raw_ip_address = "127.0.0.1";
      const unsigned short port_num = 3333;
  
      try {
          SyncTCPClient client(raw_ip_address, port_num);
  
          // Sync connect.
          client.connect();
  
          std::cout &lt;&lt; "Sending request to the server... " &lt;&lt; std::endl;
          std::string response = client.emulateLongComputationOp(10);
  
          std::cout &lt;&lt; "Response received: " &lt;&lt; response &lt;&lt; std::endl;
          sleep(2);
  
          // Close the connection and free resources.
          client.close();
      }
      catch (system::system_error &amp;e) {
          std::cout &lt;&lt; "Error occured! Error code = " &lt;&lt; e.code()
              &lt;&lt; ". Message: " &lt;&lt; e.what();
  
          return e.code().value();
      }
  
      return 0;
  }
  </code></pre>