#include <iostream>
#include <thread>
using namespace std;
//带参函数
void print(int num)
{
cout << "num=" << num << endl;
}
void test01()
{
thread t1(print, 10);
t1.join();
}
//无参函数
void print2()
{
cout << "无参" << endl;
}
void test02()
{
thread t1(print2);
t1.join();
}
//引用
//无参函数
void print3(int& num)
{
cout << "num="<<num << endl;
}
void test03()
{
int n = 20;
thread t1(print3,ref(n));//ref():转化为右值
t1.join();
}
//类的重载
class function
{
public:
void operator()()
{
cout << "重载"<<endl;
}
};
void test04()
{
function f1;
thread t1(f1);
t1.join();
}
//类中类
class functions
{
public:
void pit()
{
cout << "类中类"<<endl;
}
};
void test05()
{
functions f1;
thread t1(&functions::pit,f1);
t1.join();
}
int main()
{
test01();
test02();
test03();
test04();
test05();
return 0;
}
C++创建多线程
最新推荐文章于 2024-06-11 19:01:03 发布