C++ async_future异步高级特性使用

1. 引用头文件

#include <future>
#include <thread>
#include <chrono>  //chrono 表示 "时间" 或 "计时" 的意思
#include <random>
#include <iostream>
#include <exception>

2. 异步大致实现:

注意:future的get()只能调用一次,一次后就失效了。

int32_t DoSomething(char c)
{
    std::default_random_engine dre(c);

    //是 C++ 标准库中的一种随机数分布,它生成均匀分布的整数。也就是说,它生成的每个整数的概率是相等的。
    std::uniform_int_distribution<int> id(10, 1000);
    cout<<this_thread::get_id()<<endl;
    for(int32_t i=0; i<10; ++i)
    {
        this_thread::sleep_for(chrono::milliseconds(id(dre)));
        cout.put(c).flush();
    }
    return c;
}

int32_t Func1()
{
    return DoSomething('.');
}

int32_t Func2()
{
    return DoSomething('+');
}

int32_t AsyncAndFutureText()
{
    std::future<int32_t> result1(std::async(Func1));  //异步调用函数,future
    int32_t result2 = Func2();

    int result = result1.get() + result2;
    std::cout<<"\nFunc1() + Func2 = "<<result<<std::endl;



    auto f1 = std::async(Func1);
    auto f2 = std::async(Func2);

    if(f1.wait_for(chrono::seconds(0)) != future_status::deferred && f2.wait_for(chrono::seconds(0)) != future_status::deferred)
    {
        while (f1.wait_for(chrono::seconds(0)) != future_status::ready && f2.wait_for(chrono::seconds(0)) != future_status::ready)
        {
            ;
            this_thread::yield();
        } 
    }
    cout.put('\n').flush();
    try
    {
        f1.get();
        f2.get();
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }
    cout<<"\n Done"<<endl;

    return result;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的异步调用可以使用std::async函数来实现。std::async函数返回一个std::future对象,该对象可以用于获取异步操作的结果。std::async函数有两个参数,第一个参数是std::launch枚举类型,用于指定异步操作的启动方式,可以是std::launch::async表示异步启动,也可以是std::launch::deferred表示延迟启动;第二个参数是一个可调用对象,可以是函数指针、函数对象或者Lambda表达式等。当使用std::launch::async启动异步操作时,std::async函数会在新线程中执行该可调用对象;当使用std::launch::deferred启动异步操作时,std::async函数会在调用get()函数时执行该可调用对象。 下面是一个示例程序,展示了std::async函数的使用方法和异步启动和延迟启动的区别: ``` #include <chrono> #include <future> #include <iostream> int main() { auto begin = std::chrono::system_clock::now(); auto asyncLazy = std::async(std::launch::deferred, [] { return std::chrono::system_clock::now(); }); auto asyncEager = std::async(std::launch::async, [] { return std::chrono::system_clock::now(); }); std::this_thread::sleep_for(std::chrono::seconds(1)); auto lazyStart = asyncLazy.get() - begin; auto eagerStart = asyncEager.get() - begin; auto lazyDuration = std::chrono::duration<double>(lazyStart).count(); auto eagerDuration = std::chrono::duration<double>(eagerStart).count(); std::cout << "asyncLazy evaluated after : " << lazyDuration << " seconds." << std::endl; std::cout << "asyncEager evaluated after: " << eagerDuration << " seconds." << std::endl; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值