Why can't I call my vector of threads from the destructor? Is there some rule for using destructors?
void p ()
{
std::cout << "thread running " << std::endl;
}
class VecThreads // Error: In instantiation of member function 'std::__1::vector<std::__1::thread, std::__1::allocator<std::__1::thread> >::vector' requested here
{
public:
std::vector<std::thread> threads;
VecThreads() {
threads.push_back(std::thread(p));
}
~VecThreads()
{
threads[0].join();
}
};
int main(int argc, const char * argv[]) {
VecThreads h = VecThreads();
//h.threads[0].join(); // delete deconstructor and use this works fine
return 0;
}
The error I get:
Calling a private constructor of class 'std::__1::thread'