I am using a blocking read
in a thread.
When the program has to end, I sent a sigint
which set a flag to end the thread; but since I'm waiting in the blocking read, the thread never stops. Is there a way to wake up the read?
mFd = open(mPort.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
if(mFd == -1){
m_logger.error("Unable to open UART. Ensure it is not in use by another application");
}
struct termios settings;
tcgetattr(mFd, &settings);
cfsetispeed(&settings, mBaudrate);
settings.c_cflag &= ~PARENB; /* no parity */
settings.c_cflag &= ~CSTOPB; /* 1 stop bit */
settings.c_cflag &= ~CSIZE;
settings.c_cflag |= CS8 | CLOCAL; /* 8 bits */
/* echo off, echo newline off, canonical mode off,
* extended input processing off, signal chars off
*/
settings.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
settings.c_oflag = 0; /* Turn off output processing */
settings.c_cflag &= ~CRTSCTS;
/* Read settings :
* To understand the settings have a look at man 3 termios
* ``Canonical and noncanonical mode`` section
*/
settings.c_cc[VMIN] = 20; /* read doesn't block , one byte is enough to satisfy the read */
settings.c_cc[VTIME] = TIMEOUT_READ_BYTE_DS; /* Interbyte timer. Timeout in ds */
settings.c_cflag |= CREAD;
/* Flush Port, then applies attributes */
tcflush(mFd, TCIFLUSH);
if (tcsetattr(mFd, TCSANOW, &settings) < 0){
m_logger.error("Error during the set of the attributes to the file descriptor");
mFd= -1;
} /* apply the settings */
}
Ther read process is something like
while (EndThread==false){
memset(buffer, 0, BUFFER_SIZE);
bytesRead = read(mFd,buffer,BUFFER_SIZE); //<----- I block here until I read something
if(bytesRead < 0){
m_logger.error("Error occurred during the reading of the serial port");
}
else
{....}
I dont know if there is a signal which can wake up the read
, or if I can write on the buffer by myself in order to wake it up. I cant find any information regarding it. Can someone help me?