答案
普通函数、仿函数、lambda 表达式
栗子
#include <iostream>
#include <thread>
class CTest
{
public:
// 仿函数。
void operator()()
{
std::cout << " () " << std::endl;
return;
}
};
// 普通函数。
void func()
{
std::cout << " func " << std::endl;
return;
}
int main()
{
CTest test;
std::thread thread_1(func);
std::thread thread_2(test);
// lambda 表达式。
auto other = []{
std::cout << " other " << std::endl;
return;
};
std::thread thread_3(other);
thread_1.join();
thread_2.join();
thread_3.join();
return 0;
}
结果(不一定)
func ()
other
(SAW:Game Over!)
本文通过示例代码展示了C++中不同类型的函数如何被用作线程目标,包括普通函数、仿函数和lambda表达式。每个函数类型在创建线程时有不同的使用方式,但都能实现并行执行。
4456

被折叠的 条评论
为什么被折叠?



