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:
use pthread_cancel(): discouraged might lead unintended consequences of not clearing held resources or mutexes
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
Post a Comment