I Have a thread that reads data from a file descriptor, using select()
to block until the data is available. When the program needs to terminate, I need to close the thread, however it's blocked on select.
My question, is it a good practice to notify the thread about my intentions to terminate by closing the file-descriptor ?
is there a better method to notify the thread ? I know there's always an option to use signal, however I've read in some places that signals the pthreads
doesn't work together that well.
Well you can use a pipe, and add that to your read-set. Send a single byte on the pipe when the thread should terminate.
Or have a timeout on the select
call, and when it timeouts check for termination flag.
In addtion to the gentle ways proposed by Joachim , there also are some rough appoaches to "interrupt" the blocking select()
:
pthread_kill()
to send a signal to the blocking thread and test for (EINTR == errno)
after the select()
in question pthread_cancel()
on the thread-id. select()
belongs to the set of default cancellation points. Both methods should be considert as last ressort solutions.
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.