摄影学习网站wordpress生成16位名称

张小明 2026/1/13 0:22:32
摄影学习网站,wordpress生成16位名称,中国观鸟记录的网站架构,随机显示wordpress一 概述std::thread::join() 和 std::future::get() 都是阻塞调用#xff0c;但它们在调用顺序和线程管理上有重要区别。二 基本区别1 std::thread::join() std::thread t([](){ /* 任务 */ }); t.join(); // 等待线程结束#xff0c;但不获取返回值目的#xff1a;等待线…一 概述std::thread::join() 和 std::future::get() 都是阻塞调用但它们在调用顺序和线程管理上有重要区别。二 基本区别1 std::thread::join()std::thread t([](){ /* 任务 */ });t.join(); // 等待线程结束但不获取返回值目的等待线程执行完成。返回值无void。线程状态线程结束后线程对象变为不可 join。调用次数只能调用一次。2 std::future::get()std::futureint fut std::async(std::launch::async, [](){ return 42; });int result fut.get(); // 等待并获取结果目的等待异步操作完成并获取结果。返回值返回异步操作的结果。状态get() 后 future 变为无效。调用次数只能调用一次。三 调用顺序对比1 示例1只有 join()#include iostream#include thread#include chronovoid worker(int id) {std::this_thread::sleep_for(std::chrono::milliseconds(100));std::cout Worker id finished\n;}int main() {std::thread t1(worker, 1);std::thread t2(worker, 2);// 顺序等待t1.join(); // 阻塞等待 t1 完成t2.join(); // 阻塞等待 t2 完成std::cout All threads joined\n;return 0;}2 示例2只有 get()#include iostream#include future#include chronoint task(int x) {std::this_thread::sleep_for(std::chrono::milliseconds(x * 100));return x * 10;}int main() {auto fut1 std::async(std::launch::async, task, 3);auto fut2 std::async(std::launch::async, task, 1);// get() 会阻塞直到结果可用int result2 fut2.get(); // 先获取快的任务std::cout Result2: result2 \n;int result1 fut1.get(); // 再获取慢的任务std::cout Result1: result1 \n;return 0;}四 同时使用 join() 和 get()1 先 join() 再 get()通常不必要#include iostream#include thread#include future#include chronoint main() {std::promiseint prom;std::futureint fut prom.get_future();std::thread t([prom]() {std::this_thread::sleep_for(std::chrono::milliseconds(100));prom.set_value(42);});t.join(); // 等待线程完成int value fut.get(); // 立即获取结果因为线程已结束std::cout Value: value \n;return 0;}2 先 get() 再 join()更常见#include iostream#include thread#include future#include chronoint main() {std::promiseint prom;std::futureint fut prom.get_future();std::thread t([prom]() {std::this_thread::sleep_for(std::chrono::milliseconds(100));prom.set_value(42);});int value fut.get(); // 阻塞直到结果可用t.join(); // 立即返回因为线程已结束std::cout Value: value \n;return 0;}五 最佳实践和推荐顺序推荐先 get() 再 join()std::promiseT prom;std::futureT fut prom.get_future();std::thread t(worker_function, std::ref(prom));// 先获取结果这会等待线程完成计算T result fut.get();// 再清理线程资源t.join();优点get() 已经隐含等待线程完成的功能。join() 调用时线程已经结束会立即返回。代码逻辑更清晰。六 实际应用示例1 多任务并行计算#include iostream#include vector#include thread#include future#include numeric#include chronoint process_chunk(const std::vectorint data, int start, int end) {int sum 0;for (int i start; i end; i) {sum data[i];}std::this_thread::sleep_for(std::chrono::milliseconds(100));return sum;}int main() {std::vectorint data(1000);std::iota(data.begin(), data.end(), 1);// 创建多个futurestd::vectorstd::futureint futures;// 启动多个线程int chunk_size data.size() / 4;for (int i 0; i 4; i) {int start i * chunk_size;int end (i 3) ? data.size() : (i 1) * chunk_size;futures.push_back(std::async(std::launch::async, process_chunk,std::cref(data), start, end));}// 按任意顺序获取结果// get() 会按完成顺序阻塞std::vectorint results;for (auto fut : futures) {results.push_back(fut.get()); // 等待并获取结果}int total std::accumulate(results.begin(), results.end(), 0);std::cout Total sum: total \n;// 注意使用 std::async 时不需要显式 join// future 析构时会自动等待return 0;}2 手动管理线程和 promise#include iostream#include thread#include future#include vector#include stdexceptclass ThreadManager {std::vectorstd::thread threads;std::vectorstd::futureint futures;public:void add_task(std::functionint() task) {std::promiseint prom;futures.push_back(prom.get_future());threads.emplace_back([prom std::move(prom), task]() mutable {try {int result task();prom.set_value(result);} catch (...) {prom.set_exception(std::current_exception());}});}std::vectorint wait_all() {std::vectorint results;// 先获取所有结果for (auto fut : futures) {try {results.push_back(fut.get());} catch (const std::exception e) {std::cout Task failed: e.what() \n;results.push_back(-1);}}// 再清理所有线程for (auto t : threads) {if (t.joinable()) {t.join();}}return results;}~ThreadManager() {// 确保所有线程都已结束for (auto t : threads) {if (t.joinable()) {t.join();}}}};int main() {ThreadManager manager;manager.add_task([]() { return 1; });manager.add_task([]() { return 2; });manager.add_task([]() {throw std::runtime_error(Task failed!);return 3;});auto results manager.wait_all();for (auto r : results) {std::cout Result: r \n;}return 0;}七 重要注意事项1 异常处理std::futureint fut std::async([]() {throw std::runtime_error(Oops!);return 42;});try {int value fut.get(); // 会抛出异常} catch (const std::exception e) {std::cout Exception: e.what() \n;}2 避免死锁// 错误示例自死锁std::promiseint prom;std::futureint fut prom.get_future();std::thread t([fut, prom]() {// 这个线程在等待自己设置结果// int value fut.get(); // 死锁prom.set_value(42);});t.join();3 std::async 的特殊行为// 使用 std::launch::deferred 策略auto fut std::async(std::launch::deferred, []() {return 42;});// get() 会在调用线程中同步执行任务int value fut.get(); // 此时才执行任务4 超时等待std::futureint fut std::async([]() {std::this_thread::sleep_for(std::chrono::seconds(2));return 42;});// 使用 wait_for 非阻塞等待auto status fut.wait_for(std::chrono::seconds(1));if (status std::future_status::ready) {int value fut.get();} else {std::cout Task not ready yet\n;}八 总结1 对于纯线程管理只需 join()。2 对于需要结果的异步任务使用 std::future 和 get()。3 当同时使用 thread 和 future推荐先 get() 再 join()。这种顺序选择主要是为了代码清晰和避免重复等待因为 get() 已经隐含了等待操作完成的语义。4 使用 std::async只需 get()不需要显式 join()。九 通用模式// 如果需要结果std::futureT fut std::async(task);T result fut.get(); // 等待并获取结果// 如果只是执行后台任务std::thread t(task);t.join(); // 等待完成
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

沈阳网页建站模板南宁门户网站建设

推荐几个氛围比较好的黑客论坛社区(非常详细)零基础入门到精通,收藏这一篇就够了 FreeBuf https://www.freebuf.com 国内关注度最高的全球互联网安全媒体平台,爱好者们交流与分享安全技术的社区。 https://bbs.kanxue.com 看雪论…

张小明 2026/1/7 14:36:46 网站建设

网站建设企划书南通网站建设服务公司

AI系统可靠性设计:监控系统的7个设计要点,做到精准预警 一、引入:从一次"无预警"事故说起 2023年双11零点刚过,某头部电商的推荐系统突然陷入"诡异沉默":原本能精准推送"羽绒服"给刚浏览…

张小明 2026/1/2 0:41:17 网站建设

中小学生教育网站建设方案WordPress评论昵称显示错误

EmotiVoice 与主流 ASR 系统协同应用深度实践 在智能语音交互日益普及的今天,用户早已不再满足于“能说话”的机器助手。他们期待的是一个听得懂情绪、说得像真人的对话伙伴——这正是当前语音技术演进的核心方向。 传统语音系统中,ASR(自动语…

张小明 2026/1/5 17:10:10 网站建设

电子商务网站的建设ppt秦皇岛 免费建网站

面对成千上万的 AWS 资源,你是否曾为找不到特定实例而烦恼?SAWS(Supercharged AWS CLI)通过其革命性的智能搜索技术,彻底改变了传统的命令行操作方式。这款强大的工具不仅支持模糊匹配,还能智能理解用户意图…

张小明 2026/1/10 9:17:11 网站建设

校园网站建设网企业信息平台系统

无名杀网页版:三国杀游戏的全新体验指南 【免费下载链接】noname 项目地址: https://gitcode.com/GitHub_Trending/no/noname 无名杀网页版是一款基于浏览器运行的三国杀游戏实现,让玩家无需下载安装即可享受经典的三国杀对战乐趣。这款开源项目…

张小明 2026/1/2 6:42:00 网站建设

专业专题网站建设学院网站建设需求分析目录

Linly-Talker 镜像的故障自愈机制:让数字人系统真正“稳”起来 在虚拟主播24小时不间断直播、智能客服全年无休响应咨询的今天,我们对AI系统的期待早已超越了“能用”的范畴——它必须足够可靠。然而现实是,一个集成了大语言模型(…

张小明 2026/1/3 4:42:56 网站建设