Say I have a thread that's something like this:
void my_thread(char *device_name) {
int fd = open(device_name, O_RDONLY);
struct input_event ev;
while(1) {
read(fd, &ev, sizeof(struct input_event));
/* do something */
}
}
How do I stop such a thread? One way is using pthread_cancel
, but I'd rather do it more gracefully. Something like pthread_kill
perhaps? In such case, however, would the read method unblock (as I presume it should) and how would the thread handle the signal? Or is it the process that should handle it?
I'd be very grateful for an advice!
The answer to this is to not do anything that will block without a timeout. For IO, you shouldn't call read() until you know the call will not block. For example, use poll() or select() on the path first in order to determine the status.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With