//传递类参数,智能指针作为线程参数
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include <thread>
#include <future>
using namespace std;
void f(int n)
{
cout << n << endl;
}
std::string helloFunction(const std::string& s)
{
std::chrono::milliseconds dura(2000);
std::this_thread::sleep_for(dura);
return "Hello C++11 from " + s + ".";
}
class HelloFunctionObject
{
public:
std::string operator()(const std::string& s) const
{
std::chrono::milliseconds dura(2000);
std::this_thread::sleep_for(dura);
return "Hello C++11 from " + s + ".";
}
};
int main()
{
//用来创建异步任务
//std::async
//int n = 6;
//std::thread t(f, n + 1);
//t.join();
// 带函数的future
auto futureFunction = std::async(helloFunction, "helloFunction");
// 带函数对象的future
HelloFunctionObject helloFunctionObject;
auto futureFunction
std::future的使用
最新推荐文章于 2024-03-24 20:04:04 发布