title: Force read system call to block
body: <p>I have a program that reads from and writes to serial port. I have a reader thread that reads data and supplies informations to shared memory. The reader thread should sleep until data is available. So I want to make <code>read()</code> system call to block calling thread. Considering man pages, unless you supply <code>O_NONBLOCK</code> to <code>open</code>, <code>read</code> should always block. But I have an active thread which in it <code>read</code> returns <code>-1</code> continuously. ALso changing <code>VTIME</code> and <code>VMIN</code> does not make a difference.  This is how port is opened:</p>
  
  <pre><code>fd = open(portName.str().c_str()/*/dev/ttyS2*/, O_RDWR | O_NOCTTY);
      if (fd &lt; 0) // if open is not successful
      {
          cerr &lt;&lt; ERROR &lt;&lt; "Unable to open `" &lt;&lt; portName &lt;&lt; "'." &lt;&lt; endl;
          return false;
      }
      else
      {
          cout &lt;&lt; INFO &lt;&lt; "Port " &lt;&lt; portName.str() &lt;&lt; " successfully opened."
                  &lt;&lt; endl;
          cout &lt;&lt; INFO &lt;&lt; "Configuring port..." &lt;&lt; endl;
          fcntl(fd, F_SETFL,0);
          struct termios port_settings; // structure to store the port settings in
          cfsetispeed(&amp;port_settings, B38400); // set baud rate
          cfsetospeed(&amp;port_settings, B38400); // set baud rate
          port_settings.c_cflag |= CLOCAL | CREAD;
          port_settings.c_cflag &amp;= ~CRTSCTS; // disable H/W flow control
          port_settings.c_lflag &amp;= ~( ISIG | // disable SIGxxxx signals
                  IEXTEN | // disable extended functions
                  ECHO | ECHOE); // disable all auto-echo functions
          port_settings.c_lflag &amp;= ~ICANON ; // raw mode
          port_settings.c_oflag &amp;= ~OPOST; // raw output
          port_settings.c_iflag &amp;= ~(IXON | IXOFF | IXANY); // disable S/W flow control;
              // Following values do not change timout in runtime:
          port_settings.c_cc[VTIME] = 2; // wait 0.2 second to get data - 
          port_settings.c_cc[VMIN] = 0;
  
          port_settings.c_cflag = (port_settings.c_cflag &amp;= ~CSIZE) | CS8; // set data byte size
          port_settings.c_cflag &amp;= ~CSTOPB; // set stop bit 1
          port_settings.c_cflag &amp;= ~PARENB; // set no parity
          port_settings.c_iflag |= IGNPAR; // ignore parity
          port_settings.c_iflag &amp;= ~(INPCK | ISTRIP | PARMRK);
  
          // Set
          if (tcsetattr(fd, TCSANOW, &amp;port_settings) &lt; 0)
          {
              cerr &lt;&lt; ERROR &lt;&lt; "Unable to configure serial port." &lt;&lt; endl;
              return false;
          }
          else
          {
              cout &lt;&lt; INFO &lt;&lt; "Port `" &lt;&lt; portName.str()
                      &lt;&lt; "' configuration was successful." &lt;&lt; endl;
          }
  </code></pre>
  
  <p>And in reader thread:</p>
  
  <pre><code>byte buffer[256];
  while (true)
  {
      int packetSize = read(fd, buffer, 256);
      if (packetSize &gt; 0)
      { 
        // use data - this code is never run 
      }
      else 
      {
         // print error - we're always here. no matter how long timout is
      }
   }
  </code></pre>