23

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?

Community
  • 1
  • 1
Whyrusleeping
  • 889
  • 2
  • 9
  • 20

2 Answers2

38
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.

Griwes
  • 8,805
  • 2
  • 43
  • 70
0

I think, the problem is that you can't get pointer to member function in a way similar to functions. Here you will find more information about this.

Also, it would be much easier to answer, if you provided compipler error text.

JustSomeGuy
  • 3,677
  • 1
  • 23
  • 31