C++11 std::async 结合 lambda, std::function<>的例子

26 篇文章 0 订阅

下面是一个例子,编译通过。

#include <future>
#include <functional>
#include <iostream>
#include <thread>
#include <chrono>

using namespace std;

class Msg {
public:
    Msg(std::string name, uint32_t id) : m_name(name), m_id(id)
    {

    }

    virtual ~Msg()
    {

    }

    std::string GetName()
    {
        return m_name;
    }

    uint32_t GetId()
    {
        return m_id;
    }
private:
    std::string m_name;
    uint32_t m_id;
};

void PrintInfo(Msg *msg)
{
    for (int i = 0; i < 10; i++) {
        std::cout << msg->GetName() << msg->GetId() << std::endl;
    }
}

// function object point to the method.
std::function<void(Msg *msg)> printFunc = PrintInfo;

int main()
{
    Msg *msg =  new Msg("Hello", 999);
    std::future<void> fut = std::async(std::launch::async, PrintInfo, msg);

    Msg *msg2 = new Msg("World", 123);
    std::future<void> fut2 = std::async(std::launch::deferred, PrintInfo, msg2);

    // independent of platform.
    std::this_thread::sleep_for(std::chrono::milliseconds(10000));

    fut2.get();

    // use lambda
    std::future<bool> fut3 = std::async(std::launch::deferred, [](std::string name) -> bool {
        if (name == "s") {
            std::cout << "start!" << std::endl;
            return true;
        }
        return false;
    }, "s");

    std::this_thread::sleep_for(std::chrono::milliseconds(3000));
    fut3.get();

    // use function.
    std::future<void> fut4 = std::async(std::launch::deferred, printFunc, new Msg("Function Msg", 777));
    std::this_thread::sleep_for(std::chrono::milliseconds(3000));
    fut4.get();

    return 0;
}

编译时记得要加上-lpthread

g++ -o test_future test_future -lpthread

输出结果

Hello999
Hello999
Hello999
Hello999
Hello999
Hello999
Hello999
Hello999
Hello999
Hello999
World123
World123
World123
World123
World123
World123
World123
World123
World123
World123
start!
Function Msg777
Function Msg777
Function Msg777
Function Msg777
Function Msg777
Function Msg777
Function Msg777
Function Msg777
Function Msg777
Function Msg777

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
std::async是C++11标准库中的一个函数模板,用于创建一个异步任务,返回一个std::future对象,可以通过std::future对象获取异步任务的结果。 std::async的用法有以下几个步骤: 1. 包含头文件#include<future>。 2. 使用std::async创建异步任务,传入一个可调用对象(函数、函数指针、lambda表达式等)作为参数,标识异步任务的启动。 3. std::async会返回一个std::future对象,可以通过该对象获取异步任务的结果。 4. 可以通过std::future的成员函数get()获取异步任务的返回值,该调用会阻塞当前线程,直到异步任务完成并返回结果。也可以通过std::future的成员函数wait()等待异步任务的完成,再通过get()获取结果。 5. 可以通过std::future的成员函数valid()检查std::future对象是否可用,即异步任务是否完成并返回结果。 6. std::async还可以传入std::launch参数,显式指定异步任务的启动策略。例如,std::launch::async表示立即启动异步任务;std::launch::deferred表示延迟启动异步任务,直到调用std::future的成员函数get()或wait()时才启动。 7. 当std::async创建的异步任务完成后,std::future对象将被销毁,对std::future对象的后续操作将导致未定义的行为。 总而言之,std::async可以用于创建一个异步任务,并获得任务的返回值。它提供了一种简单的方式来进行并行编程,可以提高程序的性能和响应能力。但是需要注意的是,需要合理使用std::future对象以及与其相关的成员函数来处理异步任务的结果,避免潜在的阻塞或无效操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值