I have a class which handles synchronization of some threads.
class ThreadHandler {
std::vector<std::jthread> threads;
std::mutex mut;
std::condition_variable cv;
int some_int;
// some other synchronization related variables
};
In C++, member destructors are called in reverse order, so the destructor for cv is called before the destructor for threads. Because the destructor for threads joins the jthreads, there's a period of of time when cv and mut have been destructed and the jthreads are still running. Is that a problem?
Thanks to comments:
the asker knows the code could use these objects after they're destroyed.
So the question is: Is it okay to use an object after it's been destroyed?
And the answer is: No.
The computer doesn't care about which order you write your member variables, but it does care about using objects after they're destroyed. If the threads had no chance to use
cvandmutafter they were destroyed, then it would be okay. If they could usecvandmutafter they're destroyed, then it's not okay and you have to fix that or your program will sometimes crash.