【C++多线程系列】【四】将类的成员函数作为线程启动函数

#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");
}

线程的启动函数:

1.普通的函数

2.函数对象

3.class中的static函数

4.lammda表达式

但是,成员变量中的函数是不能作为线程的启动函数的

猜你喜欢

转载自my.oschina.net/u/3800567/blog/1798159