Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing member functions to std::thread [duplicate]

Tags:

Possible Duplicate:
Start thread with member function

I have recently been playing around with the new std::thread library in c++11 and I came across a problem. When i try to pass a classes function into a new thread, it gives me an error (I dont have the exact error text right now since im away from home) I had a class like this

class A
{
    void FunctA();
    void FunctB();

    void run()
    {
        std::thread t(FunctA);
        std::thread r(FunctB);
    }
}

What am I doing wrong?

like image 415
Whyrusleeping Avatar asked Jun 15 '12 20:06

Whyrusleeping


1 Answers

class A
{
    void FunctA();
    void FunctB();

    void run()
    {
        std::thread t(&A::FunctA, this);
        std::thread r(&A::FunctB, this);
    }
};

Pointers to member functions are different from pointers to functions, syntax of calling them is different, as well, and requires instance of class. You can just pass pointer to instance as second argument of std::thread constructor.

like image 182
Griwes Avatar answered Oct 21 '22 16:10

Griwes