title: Can not close serial port using boost asio
body: <p>I am having an issue that it seems someone else has encountered but appears to be not asked here.  I have a <em>very</em> simple application that right now just opens and closes a serial port using boost asio (just trying to do some simple debugging right now before building a larger application. The calls reside in a qt based gui.  The serial communication right now is set up in the window constructor:</p>
  
  <pre><code> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  
      QTimer *timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(getData()));
      timer-&gt;start(1000);
  
      std::string comm = "/dev/ttyUSB0";
  
      boost::asio::io_service io;
      port = std::shared_ptr&lt;boost::asio::serial_port&gt;(new   boost::asio::serial_port(io, comm));
      std::cout &lt;&lt; "Port has been successfully opened..." &lt;&lt; std::endl;
  
      boost::asio::serial_port_base::baud_rate baud(19200);
  
      port-&gt;set_option(baud);
      std::cout &lt;&lt; "Baud rate set to 19200..." &lt;&lt; std::endl;
  
  
  
      getData();
  }
  </code></pre>
  
  <p><code>getData</code> will contain calls to the serial port at regular intervals, but right now it just makes a request to populate a time stamp on the gui with the current time.</p>
  
  <p>The port is closed in the destructor</p>
  
  <pre><code>MainWindow::~MainWindow()
  {
      if (port-&gt;is_open()) port-&gt;close();
      delete ui;
  
  }
  </code></pre>
  
  <p>When the gui is closed, the destructor is called, but the system just hangs at the port closing.  It appears that it is getting stuck in boost's <code>posix_mutex.hpp</code> <code>lock</code> method.</p>
  
  <p>I have seen another post about this (<a href="https://cpc110.blogspot.com/2017/03/boost-asio-can-not-close-serial-port.html" rel="nofollow noreferrer">https://cpc110.blogspot.com/2017/03/boost-asio-can-not-close-serial-port.html</a>), but it appears that there is no answer to that one.  Does anyone have any thoughts on this?</p>
  
  <p>I am using boost 1.58 and Qt 5.8 on Ubuntu 16.04 LTS.</p>