Calling an objects member function in a thread in main()

Viewed 420

I have found a lot about creating a new thread within a class (Passing member functions to std::thread)

But is it somehow possible to do the following:

#include <iostream>
#include <thread>

using namespace std;

class myClass
{
public:
    myClass(){
    myInt = 2;
    };
    void myFun(){
        ++myInt;
    }
    int ret_myInt(){
        return myInt;
    }

private:
    int myInt;
};

int main ( void )
{
    myClass myObj_;
    std::thread t1( myObj_.myFun );                 // (1)
    t1.join();
    cout << myObj_.ret_myInt() << endl;
    return 0;
}

The Code is not working, because i cannot call a member function here (1). Is there a simple way to do that?

To be clear: I don't want to create the thread inside the member function.

2 Answers
Related