C++ 多线程-thread调用不同类型的函数

目录

thread类的构造函数概述

1. 普通函数

2. 类成员函数

 3. 仿函数

4.匿名函数

5.引用作为参数来传递

 参考


thread类的构造函数概述

thread构造函数的参数是一个可变的参数模板,可用来调用各种函数且传递若干的参数。

std::thread(可调用函数 , 若干个参数)

其中可调用函数的类型有:

1) 普通函数

2) 成员函数

3) 重载operator()运算符,即仿函数

4) lambda表达式,即匿名函数

1. 普通函数

void fun0() {
	cout << "调用普通无参函数" << endl;
}
void fun1(int n , string s) {
	cout << s << "调用普通有参函数" << n;
}
int main() {
	thread t0(fun0);
	thread t1(fun1 , 1 , "abc");
	t0.join();
	t1.join();
    return 0;
}

输出:

2. 类成员函数

class A
{
public:
	void a(int n);

};

void A::a(int n) {
	cout << "A: " << n << endl;
}
int main() {
	A aClass;
	thread aa(&A::a, &aClass, 5);
	aa.join();
}

输出:

 3. 仿函数

仿函数(Functor)又称为函数对象(Function Object)是一个能行使函数功能的类。仿函数的语法几乎和我们普通的函数调用一样,不过作为仿函数的类,都必须重载 operator() 运算符。因为调用仿函数,实际上就是通过类对象调用重载后的 operator() 运算符。(为什么仿函数必须重载()运算符)

class StringAppend
{
public:
    //关键字explicit防止发生隐式类型转换
    //:为初始化列表(把str的值赋给ss),用于初始化类成员变量的值,此过程发生在类中赋值语句执行之前
    //如本例子中变量ss是被const修饰的,该变量就只能被初始化列表赋值,而不能用赋值语句=赋值
    explicit StringAppend(const string& str) : ss(str){}    
    void operator() (const string& str) const
    {
         cout<<str<<' '<<ss<<endl;
    }
private:
    const string ss;
};

int main()
{
    //调用构造函数StringAppend,将and world赋值给ss
    StringAppend myFunctor2("and world!");
    //重载运算符(),将两个字符串一起输出
    //通过重载类里的()运算符使其调用形式很像函数调用,故称之为仿函数
    thread t1(myFunctor2,"Hello");
	t1.join();
}

输出:

4.匿名函数

 

int main() {
    //[]实际就是匿名
	std::thread t1([]() {std::cout << "hello "; });
	t1.join();

	std::thread t2([](std::string str) {std::cout << str << std::endl; }, "world");
	t2.join();

	return 0;
}

输出:

5.引用作为参数来传递

class Fctor {
public:
	// 具有一个参数 是引用,报错
	void operator() (std::string& msg) {
		msg = "wolrd";
	}
};

int main() {
	Fctor f;
	std::string m = "hello";
    //ref()就是引用
	std::thread t1(f, ref(m));

	t1.join();
	std::cout << m << std::endl;
	return 0;
}

 参考

【C++11 多线程】仔细地将参数传递给线程(三) - fengMisaka - 博客园 (cnblogs.com)

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值