I don't think this is possible directly from pthreads per se, but you can easily get around it.
Using the pthreads API, you can use pthread_cond_wait and friends to set up a "condition" and wait. When the thread is about to exit, report a status to wake the waiting thread.
Alternatively, another way is to create a pipe with pipe , and when the stream is complete, write to the pipe. On the main thread, expect either select , poll , epoll , or your favorite option at the other end of the pipe. (It also allows you to wait for other FDs at the same time.)
Newer versions of Linux also include "eventfds" to do the same, see man eventfd , but note that this is only recently added. Please note that this is not POSIX, it is only Linux, and it is only available if you are sufficiently updated. (2.6.22 or better.)
I personally always wondered why this API was not designed to handle things like file descriptors. If it were me, they would be "eventables" and you could select files, streams, timers ...
source share