zoukankan      html  css  js  c++  java
  • C++11并发编程:async,future,packaged_task,promise

    一:async

    std::async:用于创建异步任务,可以代替创建线程,函数原型:
    async(std::launch::async | std::launch::deferred, f, args...),第一个参数是创建策略
    std::launch::async:立即创建线程
    std::launch::deferred:延迟加载方式创建,知道调用future的get或者wait时才创建线程

    1 int fun4()
    2 {
    3     cout << "thread id :" << this_thread::get_id() << endl;
    4     std::this_thread::sleep_for(std::chrono::seconds(5));
    5     return 1;
    6 }
    7 
    8 std::async(fun4);

    二:future

    std::future:提供了一种访问异步操作结果机制,从异步任务中获取结果。

    获取future结果:
    1.future.get(),等待异步执行结束获取返回结果,只能调用一次
    2.future.wait(),等待异步执行完成,无返回值。
    3.future.wait_for(),超时等待返回结果,返回std::future_status状态

    future_status有三种状态:
    1.deferred:异步等待状态
    2.ready: 异步已完成状态
    3.timeout: 异步超时状态

     1     std::future_status status = future.wait_for(std::chrono::seconds(5));
     2     if (status == std::future_status::ready)
     3     {
     4         std::cout << "task is ready" << endl;
     5     }
     6     else if(status == std::future_status::timeout)
     7     {
     8         std::cout << "task is timeout" << endl;
     9     }
    10     else if (status == std::future_status::deferred)
    11     {
    12         std::cout << "task is deferred" << endl;
    13     }

    三:packaged_task

    packaged_task可以包装一个可调用的对象,可以通过异步获取调用对象的结果,返回结果传递给关联的std::future对象。

     1 int fun1(int a)
     2 {
     3     std::this_thread::sleep_for(std::chrono::seconds(5));
     4     return 10*a;
     5 }
     6 
     7 int main()
     8 {
     9     //声明一个packaged_task对象
    10     std::packaged_task<int(int)> pt1(fun1);
    11     //packaged_task与future关联
    12     std::future<int> fut1 = pt1.get_future();
    13 
    14     //创建一线程,把pt1值传入运行
    15     thread t1(std::move(pt1), 10);
    16 
    17     //阻塞获取执行结果
    18     cout << fut1.get() << endl; //注意只能get获取一次
    19 
    20     return 1;
    21 }

    四:promise

    promise可以保存一个值,并可以通过future来读取,比如一个线程把值放入promise中,另一个线程可以通过get_future来获取future,再进行get获取该值。是一种线程同步的手段。

    #include <iostream>
    #include <thread>
    #include <mutex>
    #include <future>
    
    //声明一个promise对象,用于存放整形值
    std::promise<int> prom;
    
    int fun1()
    {
        std::this_thread::sleep_for(std::chrono::seconds(5));
        prom.set_value(10);
        return 10;
    }
    
    void fun2(std::future<int> &fut)
    {
        cout << "线程1的值:" << fut.get() << endl;
        return 1;
    }
    
    int main()
    {
        thread t1(fun1);
        t1.join();
    
        //promise与future关联
        std::future<int> fut = prom.get_future();
        //引用传递future
        thread t2(fun2, std::ref(fut));
        t2.join();
        return 1;
    }

    扫码关注公众号

    专注分享Java,C/C++,STL,Spring框架,mybatis框架,mysql,redis,分布式,高并发,设计模式,爬虫,docker,shell编程等相关技术,在这里一起探讨,一起学习,一起进步,不定期分享视频书籍资源,充分利用碎片化时间,让我们的技术之路更加有乐趣。

  • 相关阅读:
    java实现验证码功能
    C# 自动注册OCX方法
    wamp出现You don’t have permission to access/on this server提示
    C# 图像旋转代码
    C# 实现图像快速 水平 垂直 翻转
    C#创建Graphics对象的方法
    winform控件大小改变是防止背景重绘导致的闪烁(转载)
    C#中DataTable中Rows.Add 和 ImportRow 对比
    MongoDb C# 驱动操作示例
    解决c#所有单线程单元(STA)线程都应使用泵式等待基元(如 CoWaitForMultipleHandles),并在运行时间很长的操作过程中定期发送消息。 转载
  • 原文地址:https://www.cnblogs.com/woniu201/p/10109364.html
Copyright © 2011-2022 走看看