赞
踩
C++11中引入了std::async
- template<class Fn, class... Args>
- future<typename result_of<Fn(Args...)>::type> async(launch policy, Fn&& fn, Args&&...args);
std::async返回一个
std::future<T>,它存储由
std::async()执行的函数对象返回的值。
函数期望的参数可以作为函数指针参数后面的参数传递给std::async()。
如果我们不指定一个启动策略,其行为将类似于std::launch::async | std::launch::deferred
本节我们将使用std::launch::async启动策略在单线程中,我们这样做:
- #include <iostream>
- #include <string>
- #include <chrono>
- #include <thread>
-
- using namespace std::chrono;
-
- std::string fetchDataFromDB(std::string recvData) {
- //确保函数要5秒才能执行完成
- std::this_thread::sleep_for(seconds(5));
-
- //处理创建数据库连接、获取数据等事情
- return "DB_" + recvData;
- }
-
- std::string fetchDataFromFile(std::string recvData) {
- //确保函数要5秒才能执行完成
- std::this_thread::sleep_for(seconds(5));
-
- //处理获取文件数据
- return "File_" + recvData;
- }
-
- int main() {
- //获取开始时间
- system_clock::time_point start = system_clock::now();
-
- //从数据库获取数据
- std::string dbData = fetchDataFromDB("Data");
-
- //从文件获取数据
- std::string fileData = fetchDataFromFile("Data");
-
- //获取结束时间
- auto end = system_clock::now();
-
- auto diff = duration_cast<std::chrono::seconds>(end - start).count();
- std::cout << "Total Time taken= " << diff << "Seconds" << std::endl;
-
- //组装数据
- std::string data = dbData + " :: " + fileData;
-
- //输出组装的数据
- std::cout << "Data = " << data << std::endl;
-
- return 0;
- }
输出:
Total Time Taken = 10 Seconds
Data = DB_Data :: File_Data
- std::future<std::string>resultFromDB = std::async(std::launch::async, fetchDataFromDB, "Data");
-
- std::string dbData = resultDromDB.get()
- #include <iostream>
- #include <string>
- #include <chrono>
- #include <thread>
- #include <future>
-
- using namespace std::chrono;
-
- std::string fetchDataFromDB(std::string recvData) {
- //确保函数要5秒才能执行完成
- std::this_thread::sleep_for(seconds(5));
-
- //处理创建数据库连接、获取数据等事情
- return "DB_" + recvData;
- }
-
- std::string fetchDataFromFile(std::string recvData) {
- //确保函数要5秒才能执行完成
- std::this_thread::sleep_for(seconds(5));
-
- //处理获取文件数据
- return "File_" + recvData;
- }
-
- int main() {
- //获取开始时间
- system_clock::time_point start = system_clock::now();
-
- std::future<std::string> resultFromDB = std::async(std::launch::async, fetchDataFromDB, "Data");
-
- //从文件获取数据
- std::string fileData = fetchDataFromFile("Data");
-
- //从DB获取数据
- //数据在future<std::string>对象中可获取之前,将一直阻塞
- std::string dbData = resultFromDB.get();
-
- //获取结束时间
- auto end = system_clock::now();
-
- auto diff = duration_cast<std::chrono::seconds>(end - start).count();
- std::cout << "Total Time taken= " << diff << "Seconds" << std::endl;
-
- //组装数据
- std::string data = dbData + " :: " + fileData;
-
- //输出组装的数据
- std::cout << "Data = " << data << std::endl;
-
- return 0;
- }
输出:
Total Time taken= 5Seconds
Data = DB_Data :: File_Data
只使用了5秒
用Function对象作为回调调用std::async
- /*
- * Function Object
- */
- struct DataFetcher {
- std::string operator ()(std::string recvdData) {
- //确保函数要5秒才能执行完成
- std::this_thread::sleep_for(seconds(5));
- //处理获取文件数据
- return "File_" + recvdData;
-
- }
- };
-
- //用函数对象调用std::async
- std::future<std::string> fileResult = std::async(DataFetcher(), "Data");
- std::future<std::string> resultFromDB = std::async([](std::string recvdData) {
-
- std::this_thread::sleep_for(seconds(5));
- //处理创建数据库连接、获取数据等事情
- return "DB_" + recvdData;
-
- }, "Data");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。