title: How to discard data as it is sent with boost::asio?
body: <p>I'm writing some code that reads and writes to serial device using <code>boost::asio</code> class. However, when sending several strings between programs, I've noticed that on the receiving program the data is read in the order as it was written to the serial port, and not as the data is sent from the other program - If I start reading data some seconds later, I don't get the values that I am sending at the moment but those that were sent previously. I'm assuming this is caused by how I am setting up my <code>boost::asio::serial_port</code>:</p>
  
  <pre><code>int main(int argc, char const *argv[]){
  
      int baud=atoi(argv[1]);
      std::string pty=argv[2];
  
      printf("Virtual device: %s\n",pty.data());
      printf("Baud rate: %d\n",baud);
  
      boost::asio::io_service io;
      boost::asio::serial_port port(io, pty);
      port.set_option(boost::asio::serial_port_base::baud_rate(baud));
  
      // counter that writes to serial port in 1s intervals
      int val=0;
      while (1){
          std::string data=std::to_string(val);
          data+='\n';
          std::cout &lt;&lt; data;
          write(port,boost::asio::buffer(data.c_str(),data.size()));
          sleep(1); 
          val++;
          data.clear();
      }
  
      port.close(); 
      return 0;
  }
  </code></pre>
  
  <p>Is there a way to force past data to be discarded as soon as a new value is sent to the serial port (which I assume should be done on the write() part of the code)?</p>