1.普通函数
(1)全局函数
void add(int a, int b, int &c);
void add(int a, int b, int &c)
{
c = a + b;
std::cout << "fun add thread id is " << std::this_thread::get_id() << std::endl;
std::cout << "&c : " << &c << " c: " << c << std::endl;
}
调用形式为:
(2)成员函数
class Calculate
{
public:
int Add(int a, int b)
{
return a + b;
}
};
2.仿函数(操作符重载函数)
3.Lambda表达式
Lamgbda表达式的语法及使用
一般将简单操作使用lambada表达式实现,可减少创建函数的开销。具体语法形式如下:
[函数对象参数] (操作符重载函数参数) mutable 或 exception 声明 -> 返回值类型 {函数体}
std::thread t([]() {
std::cout << "Hello from lamda thread " << std::this_thread::get_id() << std::endl;
});
t.detach();