简体   繁体   中英

Wake up blocking serial port read in Linux

I use the select() call to indefinitely block on a serial port fd in a dedicated thread that constantly reads data. I can't figure out a way to wake up from my select call though (unless I set a timeout, etc). In sockets programming you have the shutdown() call to wake up any threads blocked on select().

With serial ports though, I don't see any analogue. Calling close() from another thread while my 'reader' thread is blocked on select() doesn't seem to be well defined behavior on POSIX. On Linux specifically, calling close() on an fd will not wake up any threads that have called select() on that fd. From ( http://linux.die.net/man/2/select ):

If a file descriptor being monitored by select() is closed in another thread, the result is unspecified. On some UNIX systems, select() unblocks and returns, with an indication that the file descriptor is ready (a subsequent I/O operation will likely fail with an error, unless another the file descriptor reopened between the time select() returned and the I/O operations was performed). On Linux (and some other systems), closing the file descriptor in another thread has no effect on select(). In summary, any application that relies on a particular behavior in this scenario must be considered buggy.

Is it possible to wake up a thread blocked indefinitely on a serial port fd in Linux, and if so, how?

edit:

There's a 'hack'-ish way to get around this by calling select() in a loop with sleep() (this isn't ideal though since now there's a delay between a wakeup request and the thread actually waking up). Is there anything detrimental about calling select in a loop system wise?

Two typical solutions:

  1. Create pipe and add it to select queue. As soon as you send data over this pipe - select exits.

  2. If waking all threads is not a problem, you can send a signal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM