1 使用普通函数作为线程函数
代码示例:
#include <iostream>
#include <thread>
void ThreadFunction()
{
std::cout<< "线程函数被启动" << std::endl;
}
int main()
{
std::thread thread(ThreadFunction);
thread.join();
getchar();
return 0;
}
2 使用类的成员函数作为线程函数
代码示例:
#include <iostream>
#include <thread>
class ThreadFunc
{
public:
void ThreadFunction()
{
std::cout << "线程函数被启动" << std::endl;
}
};
int main()
{
ThreadFunc m_ThreadFunc;
std::thread thread(&ThreadFunc::ThreadFunction,&m_ThreadFunc);
thread.join();
getchar();
return 0;
}
3 Lambda表达式/匿名函数作为线程函数
代码示例:
#include <iostream>
#include <thread>
int main()
{
std::thread thread{ [] {std::cout << "线程函数被启动" << std::endl; } };
thread.join();
getchar();
return 0;
}
未完待续。
大家有兴趣可以访问我的个站:www.stubbornhuang.com