Is it possible to determine if a pthread_t struct I have declared is unused? I have an array of pthread_t structs, and at the end of my program I call pthread_join() on each, but if some of them are never used I don’t want to call pthread_join() on them. What’s the best way to tell if a pthread_t struct has gone unused?
Share
The
pthread_tobject passed topthread_join()must refer to a joinable thread. So you need to track this yourself – keep a flag alongside the pthread_t object to track whether it’s used (maybe package the flag in a struct with the pthread_t).Just keep in mind that in addition to not having used the
pthread_tobject yet, if you detach from or join the thread, you need to clear the flag as well (I’m not sure if this is an exhaustive list of when apthread_tobject becomes ‘non-joinable’…).