I'm writing a server in C++ that consists of a master thread that accepts incoming requests, then spawning a new std::thread object to handle the task of responding to it.
This std::thread object goes into a list of threads, and I want to clean up this list periodically like this:
For each thread in the list:
If the thread has finished executing:
Call join on the thread
Remove the thread object from the list
Now the challenge is how to determine if this thread (which is executing a void function) has hit the end or not. The joinable() function doesn't work because it returns true mid-execution as well as afterwards, and I only want my master thread to call join() if it's done, otherwise it's going to hang and wait for that child thread in serial rather than executing in parallel.