C++ Thread uses class member functions

C++ Thread uses class member functions

Sample code

#include <thread>
#include <iostream>

using std::cout;
using std::endl;
using std::thread;

class Job {
public:
  Job(int m) : _m(m){}
  void doSomeThing(int a, int b) {
    int c = a + b + _m;
    cout << "c : " << c << endl;
  }
private:
  int _m;
};

int main() {
  Job j(3);
  thread t(&Job::doSomeThing, &j, 1, 3);

  t.join();
  return 0;
}

Compile

aa: main.cpp
        g++ -O0 -g -std=c++11 main.cpp -lpthread -o aa

operation result

test@abcd:~$./aa
c:7

Guess you like

Origin blog.csdn.net/kh815/article/details/115205625