新手学C/C++:一篇代码学会使用std::future , std::promise std::async std::packaged_task完成多线程编程的回调实现

source.cpp可直接拷贝到自己的环境测试, 代码基础理论知识参考:

c++11多线程编程(八):std::future , std::promise和线程的返回值:

https://blog.csdn.net/lijinqi1987/article/details/78507623

c++11多线程编程(九):std::async介绍与实例:

https://blog.csdn.net/lijinqi1987/article/details/78909479

c++11多线程编程(十):packaged_task介绍与实例:

https://blog.csdn.net/lijinqi1987/article/details/78909524

 

source.cpp:

#include
#include
#include
#include
#include

using namespace std::chrono;

struct testDb
{
    std::string operator()(std::string recvData){
        std::this_thread::sleep_for(seconds(5));
        return "Db_" + recvData;
    }

};

struct testFile
{
    std::string operator()(std::string recvData){
        std::this_thread::sleep_for(seconds(5));
        return "File_" + recvData;
    }
};

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;
}


void fetchDataFromDB1(std::promise* promiseObj, std::string recvData) {
    //确保函数要5秒才能执行完成
    std::this_thread::sleep_for(seconds(5));

    //处理创建数据库连接、获取数据等事情
    promiseObj->set_value("DB_" + recvData);
}

void fetchDataFromFile1(std::promise* promiseObj, std::string recvData) {
    //确保函数要5秒才能执行完成
    std::this_thread::sleep_for(seconds(5));
    
    //处理获取文件数据
    promiseObj->set_value("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(end - start).count();
    //std::cout << "Total Time taken= " << diff << "Seconds" << std::endl;

    ////组装数据
    //std::string data = dbData + " :: " + fileData;
    ////输出组装的数据
    //std::cout << "Data = " << data.c_str() << std::endl;

    std::cout << "--------------------------------------------------------------------------" << std::endl;

    //c++11的方法:使用std::future 和 std::promise
    //system_clock::time_point start1 = system_clock::now();

    //std::promise promiseObj;
    //std::future futureDB = promiseObj.get_future();
    //std::thread th(fetchDataFromDB1, &promiseObj,"Data");

    //std::promise promiseFile;
    //std::future futureFile = promiseFile.get_future();
    //std::thread th1(fetchDataFromFile1, &promiseFile, "Data");

    //th.join();
    //th1.join();

    //std::string dbData1 = futureDB.get();
    //std::string fileData1 = futureFile.get();

    //auto end1 = system_clock::now();
    //auto diff1 = duration_cast(end1 - start1).count();
    //std::cout << "Total Time taken= " << diff1 << "Seconds" << std::endl;

    //std::string data1 = dbData1 + " :: " + fileData1;
    //std::cout << "Data1 = " << data1.c_str() << std::endl;

    std::cout << "--------------------------------------------------------------------------" << std::endl;

    //用lambda函数作为回调调用std::async
    /*auto start2 = system_clock::now();

    std::future futureDB2 = std::async([](std::string recvData){std::this_thread::sleep_for(seconds(5)); return "DB_" + recvData; }, "Data");
    std::future futureFile2 = std::async([](std::string recvData){    std::this_thread::sleep_for(seconds(5)); return "File_" + recvData; }, "Data");

    std::string dbData2 = futureDB2.get();
    std::string fileData2 = futureFile2.get();

    auto end2 = system_clock::now();
    auto diff2 = duration_cast(end2 - start2).count();
    std::cout << "Total Time taken= " << diff2 << "Seconds" << std::endl;
    std::string data2 = dbData2 + " :: " + fileData2;
    std::cout << "Data2 = " << data2.c_str() << std::endl;*/

    std::cout << "--------------------------------------------------------------------------" << std::endl;
    //用Function对象作为回调调用std::async
    /*auto start3 = system_clock::now();

    std::future futureDB3 = std::async(testDb(), "Data");
    std::future futureFile3 = std::async(testFile(), "Data");

    std::string dbData3 = futureDB3.get();
    std::string fileData3 = futureFile3.get();

    auto end3 = system_clock::now();
    auto diff3 = duration_cast(end3 - start3).count();
    std::cout << "Total Time taken= " << diff3 << "Seconds" << std::endl;
    std::string data3 = dbData3 + " :: " + fileData3;
    std::cout << "Data3 = " << data3.c_str() << std::endl;*/
    std::cout << "--------------------------------------------------------------------------" << std::endl;

    //使用函数指针调用std::async作为回调
    //auto start4 = system_clock::now();

    //std::future futureDB4 = std::async(std::launch::async | std::launch::deferred, fetchDataFromDB, "Data");
    //std::future futureFile4 = std::async(std::launch::async | std::launch::deferred, fetchDataFromFile, "Data");

    //std::string dbData4 = futureDB4.get();
    //std::string fileData4 = futureFile4.get();

    //auto end4 = system_clock::now();
    //auto diff4 = duration_cast(end4 - start4).count();
    //std::cout << "Total Time taken= " << diff4 << "Seconds" << std::endl;
    //std::string data4 = dbData4 + " :: " + fileData4;
    //std::cout << "Data4 = " << data4.c_str() << std::endl;
    std::cout << "--------------------------------------------------------------------------" << std::endl;

    ////使用函数指针创建packaged_task来完成回调需求
    //auto start5 = system_clock::now();
    //std::packaged_task taskDB(fetchDataFromDB);
    //std::packaged_task taskFile(fetchDataFromFile);
    //std::future futureDB5 = taskDB.get_future();
    //std::future futureFile5 = taskFile.get_future();

    //std::thread th5DB(std::move(taskDB), "Data");
    //std::thread th5File(std::move(taskFile), "Data");

    //th5DB.join();
    //th5File.join();

    //std::string dbData5 = futureDB5.get();
    //std::string fileData5 = futureFile5.get();

    //auto end5 = system_clock::now();
    //auto diff5 = duration_cast(end5 - start5).count();
    //std::cout << "Total Time taken= " << diff5 << "Seconds" << std::endl;
    //std::string data5 = dbData5 + " :: " + fileData5;
    //std::cout << "Data5 = " << data5.c_str() << std::endl;
    std::cout << "--------------------------------------------------------------------------" << std::endl;
    
    ////使用函数对象创建packaged_task来完成回调需求
    /*auto start6 = system_clock::now();
    std::packaged_task taskDB(std::move(testDb()));
    std::packaged_task taskFile(std::move(testFile()));

    std::future futureDB6 = taskDB.get_future();
    std::future futureFile6 = taskFile.get_future();

    std::thread th6DB(std::move(taskDB), "Data");
    std::thread th6File(std::move(taskFile), "Data");

    th6DB.join();
    th6File.join();

    std::string dbData6 = futureDB6.get();
    std::string fileData6 = futureFile6.get();

    auto end6 = system_clock::now();
    auto diff6 = duration_cast(end6 - start6).count();
    std::cout << "Total Time taken= " << diff6 << "Seconds" << std::endl;
    std::string data6 = dbData6 + " :: " + fileData6;
    std::cout << "Data6 = " << data6.c_str() << std::endl;*/

    std::cout << "--------------------------------------------------------------------------" << std::endl;
    
    /*auto start7 = system_clock::now();
    std::packaged_task taskDB([](std::string recvData){std::this_thread::sleep_for(seconds(5)); return "DB_" + recvData; });
    std::packaged_task taskFile([](std::string recvData) {std::this_thread::sleep_for(seconds(5)); return "File_" + recvData;});

    std::future futureDB7 = taskDB.get_future();
    std::future futureFile7 = taskFile.get_future();

    std::thread th7DB(std::move(taskDB), "Data");
    std::thread th7File(std::move(taskFile), "Data");

    th7DB.join();
    th7File.join();

    std::string dbData7 = futureDB7.get();
    std::string fileData7 = futureFile7.get();

    auto end7 = system_clock::now();
    auto diff7 = duration_cast(end7 - start7).count();
    std::cout << "Total Time taken= " << diff7 << "Seconds" << std::endl;
    std::string data7 = dbData7 + " :: " + fileData7;
    std::cout << "Data7 = " << data7.c_str() << std::endl;*/

    system("pause");
    return 0;
}
 

你可能感兴趣的:(新手学C/C++)