Is it possible to create a thread with a function pointer to a class constructor?
If this is possible, when how would the class destructor get called?
I have made this example of what i am looking for:
class ClassA
{
public:
ClassA(void* argPtr)
{ ... }
};
int main(void)
{
pthread_t thread;
pthread_create(&thread, NULL, &ClassA(), NULL);
return 0;
}
&ClassA()(if it even compiles) creates a temporary object and takes an address of that.ClassAand will call its constructor.<thread>. Pthreads are for C, and don't understand C++. And in modern C++, you can callstd::thread(&std::make_unique<ClassA>).detach(). Then the destructor will be called when theunique_ptrgoes out of scope.