c - How to gracefully make a blocking pthread exit from the main thread? -


i have pthread indefinite polling on file descriptors, negative timeout parameter.

from main thread, able indicate polling thread should exit.

i did research , found following approaches:

  1. use pthread_cancel(): discouraged might lead unintended consequences of not clearing held resources or mutexes

  2. use variable flag , set in main thread. polling thread checks flag every iteration , if set, exits calling pthread_exit(). approach won't work because polling thread doesn't loop, blocks indefinitely, waiting on file descriptors.

can suggest elegant solution problem ?

thanks! asm

you can create anonymous pipe pipe(2) , have file-descriptor-watching thread add read end polled file descriptors, exiting when file descriptor reports eof. main thread closes write end when wants thread exit.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -