[C++ multithreading series] [4] Use the member function of the class as the thread startup function

#include<iostream>
#include<thread>
using namespace std;


class A
{
public:
	A(int a):_a(a){}
	// 需要设置为静态函数,在访问成员函数
	static void f(A &a) {
		a.f2();
	}
	void f2() {
		cout << _a << endl;
	}
private:
	int _a;
};


int main(int argc, int * argv[])
{
	A a(5);
	thread t(&A::f,std::ref(a));
	t.join();

	system("pause");
}

Thread start function:

1. Ordinary functions

2. Function object

3. static function in class

4. lambda expression

 

However, the function in the member variable cannot be used as the start function of the thread

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325444304&siteId=291194637