I have the follwing three files:
MyClass.h
class MyClass {
private:
std::thread* thread = NULL;
void run();
public:
~MyClass();
void start();
}
MyClass.cpp
MyClass:~MyClass() {
if (thread != NULL)
thread -> join();
}
void MyClass::run() {
//do stuff
}
void MyClass::start() {
thread = &std::thread (&MyClass::run, this);
}
Main.cpp
int main() {
MyClass obj{};
obj.start();
return 0;
}
When I run this code I always get R6010 - abort() has been called
and I don't know why abort()
is called. When I create the thread in the main function it works, but for design reasons I want to start it in MyClass
. This there a way to start the thread in MyClass
?
P.S.: This question is quite similar, but the answers didn't solve my problem.