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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,C++ 11 引入了 std::async 函数,可以用它来创建异步任务。std::async 函数的原型如下所示: ```c++ template <class Function, class... Args> std::future<typename std::result_of<Function(Args...)>::type> async(Function&& f, Args&&... args); ``` std::async 函数的作用是创建一个异步任务,并返回一个 std::future 对象,用于获取异步任务的返回值。std::async 函数的第一个参数是要执行的函数,后面的参数是该函数的参数。该函数会在一个新线程中执行,并在执行完成后返回结果。如果该函数抛出异常,则 std::future 对象中会保存该异常信息。 std::async 函数还可以指定执行策略,例如 std::launch::async 表示在新线程中执行异步任务,std::launch::deferred 表示在调用 std::future::get() 函数时执行异步任务。如果不指定执行策略,则由编译器自行决定。 以下是一个使用 std::async 函数创建异步任务的示例: ```c++ #include <iostream> #include <future> int foo(int x) { std::cout << "foo is running in thread " << std::this_thread::get_id() << std::endl; return x + 1; } int main() { std::future<int> result = std::async(std::launch::async, foo, 1); std::cout << "main is running in thread " << std::this_thread::get_id() << std::endl; std::cout << "result is " << result.get() << std::endl; return 0; } ``` 在上面的示例中,使用 std::async 函数创建了一个异步任务 foo,并将参数 1 传递给该函数。执行结果会保存在 std::future 对象中,可以使用 std::future::get() 函数获取异步任务的返回值。在主线程中也输出了一些信息,用于对比异步任务和主线程中的执行情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值