当前位置: 首页 > news >正文

利用Qwen大模型进行c++11并发库的学习,与时俱进!!!!

文章目录

  • 1、学习新的东西可以借助ai和官方文档
    • 1.1 会问问题
  • 异步编程教程
    • 1. std::future
    • 2. std::shared_future
    • 3、std::promise
    • 4、4. std::packaged_task
    • 5. std::async
    • 6. std::future_status 和等待函数
  • 综合代码
  • 总结

1、学习新的东西可以借助ai和官方文档

因为别人写的有可能会写错或者水平不高

1.1 会问问题

eg:
在这里插入图片描述
这样写可能会讲的不清晰并且会少讲函数接口等
你可以这样问
在这里插入图片描述
这样就会清晰很多。
后续不懂最好是问ai,再参考官方文档。

异步编程教程

在这里插入图片描述

1. std::future

std::future 是一个模板类,用于访问异步操作的结果。它提供了一种机制来获取异步操作(可能在另一个线程中执行)的返回值。

常用接口
get(): 获取结果,如果结果未准备好则阻塞

valid(): 检查 future 是否拥有共享状态

wait(): 等待结果变为可用

wait_for(): 等待一段时间

wait_until(): 等待直到某个时间点

#include <iostream>
#include <future>
#include <thread>
#include <chrono>int calculate() {std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时计算return 42;
}int main() {// 使用 async 启动异步任务,返回 futurestd::future<int> fut = std::async(std::launch::async, calculate);std::cout << "正在计算结果..." << std::endl;// get() 会阻塞直到结果可用int result = fut.get();std::cout << "结果是: " << result << std::endl;// 再次调用 get() 会导致异常,因为共享状态已被消费// int result2 = fut.get(); // 错误!return 0;
}

2. std::shared_future

在这里插入图片描述

std::shared_future 类似于 std::future,但可以被多次访问(允许多个线程等待同一个结果)。

常用接口
与 std::future 类似,但可以多次调用 get()

#include <iostream>
#include <future>
#include <thread>
#include <vector>void worker(std::shared_future<int> fut) {// 每个线程都可以安全地获取结果int result = fut.get();std::cout << "Worker got result: " << result << std::endl;
}int main() {// 创建一个 promise 对象std::promise<int> prom;// 从 promise 获取 futurestd::future<int> fut = prom.get_future();// 将 future 转换为 shared_futurestd::shared_future<int> shared_fut = fut.share();// 创建多个线程共享同一个结果std::vector<std::thread> threads;for (int i = 0; i < 3; ++i) {threads.emplace_back(worker, shared_fut);}// 设置 promise 的值prom.set_value(42);// 等待所有线程完成for (auto& t : threads) {t.join();}return 0;
}

3、std::promise

std::promise 是一个模板类,用于存储一个值或异常,稍后可以通过与之关联的 std::future 对象获取。

常用接口
get_future(): 获取与 promise 关联的 future

set_value(): 设置结果值

set_exception(): 设置异常

set_value_at_thread_exit(): 在线程退出时设置值

set_exception_at_thread_exit(): 在线程退出时设置异常
在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <future>
#include <thread>
#include <stdexcept>void calculate(std::promise<int> prom) {try {// 模拟计算std::this_thread::sleep_for(std::chrono::seconds(1));int result = 42;// 设置结果值prom.set_value(result);} catch (...) {// 捕获所有异常并存储在 promise 中prom.set_exception(std::current_exception(
http://www.xdnf.cn/news/1309645.html

相关文章:

  • AI安全增强核心技术:提示词防火墙、置信度过滤与知识蒸馏防御
  • 第6问 数据分析领域主要的岗位有哪些?
  • Rust 入门 KV存储HashMap (十七)
  • pdf合并代码
  • 【C++】异常详解(万字解读)
  • FPGA串口通信实现方案
  • Qt QDateTime时间部分显示为全0,QTime赋值后显示无效问题【已解决】
  • 【C++】C++11
  • Maven私服配置模版
  • 深入详解PCB布局布线技巧-去耦电容的摆放位置
  • IOMMU的2级地址翻译机制及多级(2~5)页表查找
  • Python 项目高频设计模式实战指南:从理念到落地的全景剖析
  • 电路方案分析(二十一)笔记本电脑散热风扇参考设计
  • 【运维心得】三步更换HP笔记本电脑外壳
  • 玄机靶场 | 日志分析-Tomcat日志分析
  • Tomcat架构深度解析:从Server到Servlet的全流程揭秘
  • Jenkins常见问题及解决方法
  • js原生实现手写签名与使用signature_pad库实现手写签名
  • 【科研绘图系列】R语言在DOM再矿化数据分析与可视化中的应用
  • 【CF】Day128——杂题 (图论 + 贪心 | 集合 + 贪心 + 图论 | 二分答案 + 贪心)
  • bev 感知算法 近一年来的新进展
  • echarts 画一个饼图,并且外围有一个旋转动画
  • pytest tmpdir fixture介绍(tmpdir_factory)(自动在测试开始前创建一个临时目录,并在测试结束后删除该目录)
  • 【LeetCode题解】LeetCode 35. 搜索插入位置
  • flowable汇总查询方式
  • ktg-mes 改造成 Saas 系统
  • Golang分布式事务处理方案
  • ROS move_base 混合功能导航 RealSense D435i + 3D 点云地图 + 楼层切换 + 路径录制 + 路径规划
  • 适合2D而非3D的游戏
  • Rust学习笔记(四)|结构体与枚举(面向对象、模式匹配)