0

0

c++如何使用std::future和std::promise_c++异步编程future/promise指南

尼克

尼克

发布时间:2025-09-18 10:50:01

|

941人浏览过

|

来源于php中文网

原创

std::future和std::promise用于C++异步编程,前者获取结果,后者设置结果;通过创建promise、获取future、启动线程、设置值或异常、最后get获取结果实现;get阻塞可用wait_for避免;异常通过set_exception传递;shared_future允许多次get;async封装了future/promise简化使用;需注意数据线程安全及promise生命周期。

c++如何使用std::future和std::promise_c++异步编程future/promise指南

C++ 中

std::future
std::promise
是实现异步编程的关键工具。它们允许你在不同的线程之间传递数据和状态,而无需显式地使用锁或条件变量。简单来说,
std::promise
负责设置异步操作的结果,而
std::future
负责获取这个结果。

解决方案

要使用

std::future
std::promise
进行异步编程,你需要遵循以下步骤:

  1. 创建

    std::promise
    对象:
    std::promise
    对象用于设置异步操作的结果。你需要指定结果的类型。例如,如果你要异步计算一个整数,可以创建一个
    std::promise
    对象。

    立即学习C++免费学习笔记(深入)”;

  2. 获取

    std::future
    对象:
    std::promise
    对象中获取一个
    std::future
    对象。
    std::future
    对象允许你稍后获取异步操作的结果。

  3. 启动异步任务 将异步任务提交给一个新线程或线程池。在这个任务中,你需要使用

    std::promise
    对象来设置结果。

  4. 设置结果: 在异步任务中,使用

    std::promise::set_value()
    方法设置结果。如果异步任务抛出异常,可以使用
    std::promise::set_exception()
    方法设置异常。

  5. 获取结果: 在主线程中,使用

    std::future::get()
    方法获取异步操作的结果。
    get()
    方法会阻塞,直到结果可用。如果异步任务抛出了异常,
    get()
    方法会重新抛出该异常。

下面是一个简单的示例:

#include 
#include 
#include 

int calculate_sum(int a, int b) {
    // 模拟耗时操作
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return a + b;
}

int main() {
    std::promise promise;
    std::future future = promise.get_future();

    std::thread t([&promise]() {
        try {
            int result = calculate_sum(5, 3);
            promise.set_value(result);
        } catch (...) {
            promise.set_exception(std::current_exception());
        }
    });

    try {
        int sum = future.get();
        std::cout << "Sum: " << sum << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    }

    t.join();

    return 0;
}

副标题1

std::future
get()
方法会阻塞,如何避免阻塞?

可以使用

std::future::wait_for()
std::future::wait_until()
方法来避免无限期阻塞。这些方法允许你指定一个超时时间。如果在超时时间内结果不可用,它们会返回一个
std::future_status
值,指示超时或结果已准备好。

#include 
#include 
#include 
#include 

int main() {
    std::promise promise;
    std::future future = promise.get_future();

    std::thread t([&promise]() {
        std::this_thread::sleep_for(std::chrono::seconds(3));
        promise.set_value(42);
    });

    // 等待最多2秒
    auto status = future.wait_for(std::chrono::seconds(2));

    if (status == std::future_status::ready) {
        std::cout << "Result: " << future.get() << std::endl;
    } else if (status == std::future_status::timeout) {
        std::cout << "Timeout: Result not available yet." << std::endl;
    } else {
        std::cout << "Deferred." << std::endl;
    }

    t.join();
    return 0;
}

副标题2

燕雀Logo
燕雀Logo

为用户提供LOGO免费设计在线生成服务

下载

std::promise
std::future
如何处理异常?

std::promise
允许你使用
set_exception()
方法设置一个异常。
std::future
get()
方法会重新抛出这个异常。这允许你将异步操作中的异常传递回主线程。
std::current_exception()
可以捕获当前线程的异常并将其存储起来,以便稍后通过
std::promise
传递。

#include 
#include 
#include 
#include 

int main() {
    std::promise promise;
    std::future future = promise.get_future();

    std::thread t([&promise]() {
        try {
            throw std::runtime_error("Something went wrong in the thread!");
        } catch (...) {
            promise.set_exception(std::current_exception());
        }
    });

    try {
        future.get(); // 这会抛出 std::runtime_error
    } catch (const std::runtime_error& e) {
        std::cerr << "Caught exception: " << e.what() << std::endl;
    }

    t.join();
    return 0;
}

副标题3

std::shared_future
std::future
有什么区别?什么时候应该使用
std::shared_future

std::future
只能被
get()
调用一次,之后就失效了。
std::shared_future
可以被多个线程共享,并且每个线程都可以多次调用
get()
(或者
wait()
等)。

如果你需要多个线程访问同一个异步操作的结果,应该使用

std::shared_future
std::shared_future
允许你创建多个对同一个结果的引用。

#include 
#include 
#include 
#include 

int main() {
    std::promise promise;
    std::future future = promise.get_future();
    std::shared_future shared_future = future.share();

    std::vector threads;
    for (int i = 0; i < 3; ++i) {
        threads.emplace_back([shared_future, i]() {
            std::cout << "Thread " << i << ": " << shared_future.get() << std::endl;
        });
    }

    promise.set_value(123);

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

    return 0;
}

副标题4

std::async
std::future/std::promise
的关系是什么?

std::async
是一个高层次的异步操作启动函数,它内部使用了
std::future
std::promise
。当你调用
std::async
时,它会自动创建一个
std::promise
,启动一个异步任务,并将
std::promise
关联的
std::future
返回给你。
std::async
简化了异步编程,你不需要手动创建
std::promise
std::thread
。但是,如果你需要更精细的控制(例如,自定义线程池或异常处理),那么手动使用
std::future
std::promise
可能会更好。

#include 
#include 

int calculate_product(int a, int b) {
    std::cout << "Calculating product in a separate thread." << std::endl;
    return a * b;
}

int main() {
    std::future future = std::async(std::launch::async, calculate_product, 7, 6);

    std::cout << "Waiting for the result..." << std::endl;
    int product = future.get();
    std::cout << "Product: " << product << std::endl;

    return 0;
}

副标题5

使用

std::future
std::promise
时需要注意哪些线程安全问题?

std::promise
std::future
本身是线程安全的,但你需要注意它们所操作的数据的线程安全性。例如,如果你在异步任务中修改一个共享变量,你需要使用锁来保护这个变量。另外,确保在
std::promise
对象销毁之前设置结果或异常,否则会导致未定义行为。 避免在多个线程中同时调用同一个
std::promise
对象的
set_value
set_exception
方法。

相关专题

更多
string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

338

2023.08.02

int占多少字节
int占多少字节

int占4个字节,意味着一个int变量可以存储范围在-2,147,483,648到2,147,483,647之间的整数值,在某些情况下也可能是2个字节或8个字节,int是一种常用的数据类型,用于表示整数,需要根据具体情况选择合适的数据类型,以确保程序的正确性和性能。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

542

2024.08.29

c++怎么把double转成int
c++怎么把double转成int

本专题整合了 c++ double相关教程,阅读专题下面的文章了解更多详细内容。

53

2025.08.29

C++中int的含义
C++中int的含义

本专题整合了C++中int相关内容,阅读专题下面的文章了解更多详细内容。

197

2025.08.29

线程和进程的区别
线程和进程的区别

线程和进程的区别:线程是进程的一部分,用于实现并发和并行操作,而线程共享进程的资源,通信更方便快捷,切换开销较小。本专题为大家提供线程和进程区别相关的各种文章、以及下载和课程。

482

2023.08.10

线程和进程的区别
线程和进程的区别

线程和进程的区别:线程是进程的一部分,用于实现并发和并行操作,而线程共享进程的资源,通信更方便快捷,切换开销较小。本专题为大家提供线程和进程区别相关的各种文章、以及下载和课程。

482

2023.08.10

Java 并发编程高级实践
Java 并发编程高级实践

本专题深入讲解 Java 在高并发开发中的核心技术,涵盖线程模型、Thread 与 Runnable、Lock 与 synchronized、原子类、并发容器、线程池(Executor 框架)、阻塞队列、并发工具类(CountDownLatch、Semaphore)、以及高并发系统设计中的关键策略。通过实战案例帮助学习者全面掌握构建高性能并发应用的工程能力。

61

2025.12.01

promise的用法
promise的用法

“promise” 是一种用于处理异步操作的编程概念,它可以用来表示一个异步操作的最终结果。Promise 对象有三种状态:pending(进行中)、fulfilled(已成功)和 rejected(已失败)。Promise的用法主要包括构造函数、实例方法(then、catch、finally)和状态转换。

301

2023.10.12

AO3中文版入口地址大全
AO3中文版入口地址大全

本专题整合了AO3中文版入口地址大全,阅读专题下面的的文章了解更多详细内容。

1

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
C# 教程
C# 教程

共94课时 | 7.2万人学习

C 教程
C 教程

共75课时 | 4.2万人学习

C++教程
C++教程

共115课时 | 13.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号