网站开发嫌工时长,wordpress软件门户主题,wordpress插件看访问者数量,郑州做花店网站C 标准库中的 future 和 async 提供了一种便捷的方式来实现并发编程。std::async 是一个非常强大的工具#xff0c;它可以用于启动异步任务#xff0c;并返回一个 std::future 对象#xff0c;该对象可以用来等待任务的结果。
std::async 的基本用法
std::async 用于启动一…C 标准库中的 future 和 async 提供了一种便捷的方式来实现并发编程。std::async 是一个非常强大的工具它可以用于启动异步任务并返回一个 std::future 对象该对象可以用来等待任务的结果。
std::async 的基本用法
std::async 用于启动一个异步任务。它的基本语法如下
#include iostream
#include future
#include thread// 一个简单的函数模拟一些工作
int work(int x) {std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟延迟return x * x;
}int main() {// 使用 std::async 启动异步任务并获取 std::future 对象std::futureint result std::async(std::launch::async, work, 10);// 在此处可以执行其他工作std::cout Doing other work... std::endl;// 获取异步任务的结果会阻塞直到结果准备好int value result.get();std::cout Result: value std::endl;return 0;
}std::async 启动模式
std::async 提供了两种启动模式
std::launch::async任务在独立线程中异步执行。std::launch::deferred任务延迟执行直到 future 对象的 get 或 wait 方法被调用。
示例不同的启动模式
#include iostream
#include future
#include threadint work(int x) {std::this_thread::sleep_for(std::chrono::seconds(2));return x * x;
}int main() {// 异步启动std::futureint async_result std::async(std::launch::async, work, 10);// 延迟启动std::futureint deferred_result std::async(std::launch::deferred, work, 20);// 在此处可以执行其他工作std::cout Doing other work... std::endl;// 获取异步任务的结果会阻塞直到结果准备好int async_value async_result.get();std::cout Async result: async_value std::endl;// 获取延迟任务的结果会在此时执行任务int deferred_value deferred_result.get();std::cout Deferred result: deferred_value std::endl;return 0;
}使用 std::future 获取结果
std::future 提供了多种方法来获取任务结果
get阻塞当前线程直到任务完成并返回结果。wait阻塞当前线程直到任务完成。wait_for阻塞当前线程一段时间等待任务完成。wait_until阻塞当前线程直到指定时间等待任务完成。
示例使用 std::future 的不同方法
#include iostream
#include future
#include threadint work(int x) {std::this_thread::sleep_for(std::chrono::seconds(2));return x * x;
}int main() {std::futureint result std::async(std::launch::async, work, 10);// 等待一段时间if (result.wait_for(std::chrono::seconds(1)) std::future_status::timeout) {std::cout Task is still running... std::endl;}// 再次等待直到任务完成result.wait();int value result.get();std::cout Result: value std::endl;return 0;
}捕获异常
如果异步任务在执行过程中抛出异常std::future 的 get 方法会重新抛出该异常。可以通过捕获异常来处理任务中的错误。
示例捕获异常
#include iostream
#include future
#include stdexcept
#include threadint work(int x) {if (x 0) {throw std::invalid_argument(x must be non-negative);}std::this_thread::sleep_for(std::chrono::seconds(2));return x * x;
}int main() {// 启动一个会抛出异常的任务std::futureint result std::async(std::launch::async, work, -10);try {int value result.get(); // 这里会重新抛出异常std::cout Result: value std::endl;} catch (const std::exception e) {std::cout Exception: e.what() std::endl;}return 0;
}总结
std::async用于启动异步任务可以指定启动模式async 或 deferred。std::future用于获取异步任务的结果提供多种方法get, wait, wait_for, wait_until。异常处理通过 std::future 的 get 方法可以捕获异步任务中抛出的异常。