mardi 14 avril 2020

C++ Creating a thread with non static member function

I'm trying to create a new thread in c++ with a non static member function of a class. However this seems to be crashing consistently on a mac and I can't figure out why. Here's a minimal example:

class A {
public:
    void hello() {
        cout << "hello" << endl;
    }

    A() {
        cout << "As constructor" << endl;
        // thread(&hello, this);
    }

    void start() {
        thread(&A::hello, this);
    }
};

int main(){
    A test;
    test.start();

}

I'm compiling on a mac with this:

clang++ -std=c++11 -stdlib=libc++ -pthread -o hello thread.cpp 

What am I missing?

Aucun commentaire:

Enregistrer un commentaire