#include <thread>
#include <iostream>
#include <string>
using namespace std;
void func(int& a)
{
a++;
}
int main()
{
int a = 42;
thread t(func, ref(a));//如果需要将参数按引用进行传递,那么就一定要把该参数封装到std::ref或者std::cref之中。
t.join();
cout << a << endl;
system("pause");
return 0;
}
#include <thread>
#include <iostream>
#include <string>
using namespace std;
void func(int& a)
{
a++;
}
int main()
{
int a = 42;
thread t(func, a);//不是将a封装到std::ref之中的话,输出的将是42。
t.join();
cout << a << endl;
system("pause");
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void func(int& a)
{
a++;
}
int main()
{
int a = 42;
thread t(func, ref(a));//如果需要将参数按引用进行传递,那么就一定要把该参数封装到std::ref或者std::cref之中。
t.join();
cout << a << endl;
system("pause");
return 0;
}
#include <thread>
#include <iostream>
#include <string>
using namespace std;
void func(int& a)
{
a++;
}
int main()
{
int a = 42;
thread t(func, a);//不是将a封装到std::ref之中的话,输出的将是42。
t.join();
cout << a << endl;
system("pause");
return 0;
}