异步编程之std::future(一): 使用

目录

1.概述

2.std::future的基本用法

3.使用 std::shared_future

4.std::future的使用场景

5.总结


1.概述

        在编程实践中,我们常常需要使用异步调用。通过异步调用,我们可以将一些耗时、阻塞的任务交给其他线程来执行,从而保证当前线程的快速响应能力。还有一些场景可以通过将一个任务,分成多个部分然后将这部分交给多个线程来进行并发执行,从而完成任务的快速计算执行,提高应用性能。这里就需要用到异步调用的概念。

        异步调用,就是当前线程将一个任务交给另外一个线程来进行执行,当前线程会接着执行当前任务,不需要等待这个交付给其他线程执行的任务的结果,直到其在未来的某一个时刻需要使用这个任务执行结果数据的时候。

        std::future 是 C++11 标准库中引入的一个非常重要的特性,它提供了一种机制来访问异步操作的结果。当你启动一个异步操作(比如,通过 std::asyncstd::packaged_taskstd::promise 等)时,你可以立即获得一个 std::future 对象,这个对象将在异步操作完成时持有操作的结果。

2.std::future的基本用法

要理解 std::future 的用法,我们需要先了解几个相关的组件:

  • std::async:用于启动异步任务,并返回一个 std::future 对象。

  • std::promise:用于设置将来某个时刻的值,可以与 std::future 配合使用。

  • std::future:用于获取异步操作的结果。

1)使用 std::async 创建异步任务

std::async 是最简单、最直接的创建异步任务的方式。我们来看一个简单的例子:

#include <iostream>
#include <future>
#include <chrono>

int main() {
    // 启动一个异步任务,计算某个复杂的结果
    std::future<int> result = std::async(std::launch::async, []() -> int {
        std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作
        return 42;
    });

    std::cout << "Doing something else while waiting for the result..." << std::endl;

    // 等待并获取结果
    int value = result.get();
    std::cout << "Result: " << value << std::endl;

    return 0;
}

        在这个例子中,我们使用 std::async 启动了一个异步任务,该任务在后台计算一个结果并返回一个 std::future 对象。我们可以继续执行其他操作,直到需要获取异步任务的结果时,调用 result.get() 阻塞等待并获取结果。

2)使用 std::promise 和 std::future

        除了 std::async,我们还可以使用 std::promise 和 std::future 来实现更灵活的异步任务管理。std::promise 用于在某个时刻设置结果,而 std::future 用于在稍后获取该结果。来看一个具体的例子:

#include <iostream>
#include <future>
#include <thread>

void compute(std::promise<int>& prom) {
    std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作
    prom.set_value(42); // 设置结果
}

int main() {
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();

    std::thread t(compute, std::ref(prom));

    std::cout << "Doing something else while waiting for the result..." << std::endl;

    int value = fut.get();
    std::cout << "Result: " << value << std::endl;

    t.join();

    return 0;
}

        在这个例子中,我们创建了一个 std::promise 对象,并通过 get_future() 获取与之关联的 std::future 对象。然后,我们启动一个线程,在该线程中执行耗时操作并设置结果。主线程可以继续执行其他操作,直到调用 fut.get() 阻塞等待并获取结果。

3)std::future的尝试等待

std::future 还提供了 wait_for() 和 wait_until() 方法,允许你尝试等待一段时间或直到某个时间点。这些方法不会永久阻塞线程,而是会在指定的时间或条件到达时返回。如:

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

int async_task(int id) {
    std::this_thread::sleep_for(std::chrono::seconds(id));
    return id * id;
}

int main() {
    std::vector<std::future<int>> futures;

    for (int i = 1; i <= 3; ++i) {
        futures.push_back(std::async(std::launch::async, async_task, i));
    }

    for (auto& fut : futures) {
        std::cout << "Result: " << fut.get() << std::endl;
    }

    return 0;
}

4)std::future的错误处理

        如果异步操作中抛出了异常,并且该异常没有被捕获,那么当你调用 get() 方法时,这个异常会被重新抛出。因此,你需要准备好处理这些可能的异常。如:

#include <iostream>
#include <future>
#include <thread>

void compute(std::promise<int>& prom) {
    try {
        std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作
        throw std::runtime_error("Something went wrong"); // 抛出异常
        prom.set_value(42); // 设置结果
    } catch (...) {
        prom.set_exception(std::current_exception()); // 设置异常
    }
}

int main() {
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();

    std::thread t(compute, std::ref(prom));

    std::cout << "Doing something else while waiting for the result..." << std::endl;

    try {
        int value = fut.get();
        std::cout << "Result: " << value << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Caught exception: " << e.what() << std::endl;
    }

    t.join();

    return 0;
}

        在这个例子中,我们在 compute 函数中抛出了一个异常,并在 std::promise 对象中设置该异常。主线程在调用 fut.get() 时,会捕获并处理这个异常。 

注意事项

  • 每个 std::future 对象只能调用一次 get() 方法。
  • 一旦 std::future 对象的 get() 方法被调用并返回了结果,或者 std::future 对象被移动,它就不能再用来获取结果了。
  • 如果异步操作抛出异常且该异常在 std::future 对象被销毁前未被捕获,则程序将终止。因此,请确保在销毁 std::future 对象之前调用 get() 方法并适当处理异常。

3.使用 std::shared_future

        有时候,我们希望多个线程可以共享一个 std::future 对象的结果。C++11 引入了 std::shared_future,它允许多个线程安全地访问同一个异步操作的结果。来看一个例子:

#include <iostream>
#include <future>
#include <thread>
#include <vector>

void compute(std::shared_future<int> fut) {
    std::cout << "Thread got result: " << fut.get() << std::endl;
}

int main() {
    std::promise<int> prom;
    std::shared_future<int> fut = prom.get_future().share();

    std::vector<std::thread> threads;

    for (int i = 0; i < 3; ++i) {
        threads.emplace_back(compute, fut);
    }

    std::this_thread::sleep_for(std::chrono::seconds(2));
    prom.set_value(42);

    for (auto& t : threads) {
        t.join();
    }

    return 0;
}

        在这个例子中,我们使用 std::promise 创建了一个 std::shared_future 对象,并将其传递给多个线程。这些线程可以共享并安全地访问同一个异步操作的结果。

4.std::future的使用场景

  • 并行计算:当你需要将计算任务分配给多个线程并行执行,并需要等待所有任务完成时。
  • 异步IO:在进行耗时的IO操作时,可以使用异步方式执行,并在操作完成时继续处理。
  • 性能优化:通过异步处理非关键路径上的操作,可以提高应用程序的响应性和吞吐量。

5.总结

   std::future 是 C++ 中进行异步编程的重要工具,它简化了异步操作的启动、结果查询和异常处理。通过合理使用 std::future 和相关机制,可以编写出既高效又易于维护的异步代码。

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值