-1

So I'm trying to thread a member function but while using two different objects of a class (each object has a variable initialized to a different value). I know how to use multithreading if I want to pass a member function inside a class such as:

class ClassA{

public:
void func{}
}

int main{

thread t1(&ClassA func);
thread t2(&ClassA func);
}

Is there a way to use multithreading while referencing to a specific object under a class? I couldn't find anything online specific to this question.

For example we have:

class ClassA{

public:
ClassA(sf::texture tex) : sprite(tex){};
void func(){
//does sth with sprite for this object
}
private:
sf::sprite sprite;
};

int main{
ClassA class1(tex1);
ClassA class2(tex2);

//thread t1(&ClassA func, What should go here?)
//thread t2(&ClassA func, What should go here?)
}

I want to call func of respective class1 and class2 so that they both can use their own initialized tex.

Starior
  • 99
  • Maybe this could help: [enter link description here](https://stackoverflow.com/questions/10673585/start-thread-with-member-function) – Radu Muntean Dec 06 '18 at 08:36
  • Possible duplicate of [Start thread with member function](https://stackoverflow.com/questions/10673585/start-thread-with-member-function) – super Dec 06 '18 at 08:41

2 Answers2

3

There's two ways to really go about doing this.

I prefer the lambda approach, which lets you add some sometimes-necessary complexity that isn't part of func:

#include <thread>

class ClassA {
 public:
  ClassA(const char* c) : c_(c){};
  void func() {
    // Do thing.
  }

 private:
  const char* c_;
};

int main(int argc, char* argv[]) {
  ClassA obj1("HELLO");
  ClassA obj2("WORLD");

  std::thread t1([&obj1]() { obj1.func(); });
  std::thread t2([&obj2]() { obj2.func(); });

  t1.join();
  t2.join();
}

But there's also the pointer-to-member-function approach:

#include <thread>

class ClassA {
 public:
  ClassA(const char* c) : c_(c) {};
  void func() {
    // Do thing.
  }

 private:
  const char* c_;
};

int main(int argc, char* argv[]) {
  ClassA obj1("HELLO");
  ClassA obj2("WORLD");

  std::thread t1(&ClassA::func, &obj1);
  std::thread t2(&ClassA::func, &obj2);

  t1.join();
  t2.join();
}
druckermanly
  • 2,694
  • 15
  • 27
1

Since C++11, wrappers for functions and member functions can be used. Together with std::bind, you get "something" that represents a member function already bound to a particular object and - optionally - further parameters:

class MyClass {
public:

    void printX() const { cout << x << endl; };
    int x;
};

int main(){

    MyClass mco1 {1}, mco2 {2};

    auto f = std::bind(&MyClass::printX, &mco1);
    std::thread t1 (f);

    f = std::bind(&MyClass::printX, &mco2);
    std::thread t2 (f);


    t1.join();
    t2.join();

}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • 1
    Using `std::bind` is not wrong, but generally lambdas are to be preferred. – super Dec 06 '18 at 09:55
  • @super: What's the advantage of a lambda over a bound function in this concrete example? – Stephan Lechner Dec 06 '18 at 22:31
  • Performance is one reason. Readability and less error prone, but that is more of an opinion. It's talked about in [this cppcon talk](https://www.youtube.com/watch?v=zt7ThwVfap0). So in this concrete example there is probably no difference except preference. – super Dec 06 '18 at 23:22