C++并发编程之异常安全性增强

在并发编程中,异常安全是一个非常重要的方面,因为并发环境下的错误处理比单线程环境更加复杂。当多个线程同时执行时,异常不仅可能影响当前线程,还可能影响其他线程和整个程序的稳定性。以下是一些增强并发程序异常安全性的方法,并附有示例代码。

1. 异常捕获和处理

在多线程程序中,每个线程都应该有自己的异常捕获机制。常见的做法是在每个线程的入口点(如线程函数)中使用 try-catch 块来捕获和处理异常。

示例代码:
#include <iostream>
#include <thread>
#include <exception>void threadFunction() {try {// 模拟可能抛出异常的代码throw std::runtime_error("An error occurred in the thread");} catch (const std::exception& e) {std::cerr << "Exception caught in thread: " << e.what() << std::endl;// 可以在这里进行日志记录、资源清理等操作}
}int main() {std::thread t(threadFunction);t.join();return 0;
}

2. 资源管理

使用 RAII(Resource Acquisition Is Initialization)技术来管理资源,确保资源在异常情况下也能正确释放。C++ 中的智能指针(如 std::unique_ptr 和 std::shared_ptr)和 std::lock_guard 等都是 RAII 的典型应用。

示例代码:
#include <iostream>
#include <thread>
#include <memory>
#include <mutex>std::mutex mtx;void threadFunction() {try {std::unique_ptr<int> resource(new int(42));std::lock_guard<std::mutex> lock(mtx);// 模拟可能抛出异常的代码throw std::runtime_error("An error occurred in the thread");} catch (const std::exception& e) {std::cerr << "Exception caught in thread: " << e.what() << std::endl;}
}int main() {std::thread t(threadFunction);t.join();return 0;
}

3. 线程同步

在多线程环境中,确保线程间的同步非常重要。使用互斥锁、条件变量等同步原语时,要确保在异常情况下不会导致死锁或资源泄露。

示例代码:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>std::mutex mtx;
std::condition_variable cv;
bool ready = false;void prepare() {try {std::this_thread::sleep_for(std::chrono::milliseconds(1000));std::lock_guard<std::mutex> lock(mtx);ready = true;cv.notify_one();} catch (const std::exception& e) {std::cerr << "Exception caught in prepare: " << e.what() << std::endl;// 可以在这里进行日志记录、资源清理等操作}
}void waitAndPrint() {try {std::unique_lock<std::mutex> lock(mtx);cv.wait(lock, [] { return ready; });std::cout << "Ready is true" << std::endl;} catch (const std::exception& e) {std::cerr << "Exception caught in waitAndPrint: " << e.what() << std::endl;}
}int main() {std::thread t1(prepare);std::thread t2(waitAndPrint);t1.join();t2.join();return 0;
}

4. 异常传播

在多线程环境中,异常可能需要从一个线程传播到另一个线程。可以使用 std::promise 和 std::future 来实现异常的跨线程传播。

示例代码:
#include <iostream>
#include <thread>
#include <future>void threadFunction(std::promise<int> promise) {try {// 模拟可能抛出异常的代码throw std::runtime_error("An error occurred in the thread");promise.set_value(42);} catch (const std::exception& e) {promise.set_exception(std::current_exception());}
}int main() {std::promise<int> promise;std::future<int> future = promise.get_future();std::thread t(threadFunction, std::move(promise));t.join();try {int value = future.get();std::cout << "Value: " << value << std::endl;} catch (const std::exception& e) {std::cerr << "Exception caught in main: " << e.what() << std::endl;}return 0;
}

5. 日志记录

在多线程程序中,记录详细的日志是诊断问题的重要手段。可以使用日志库(如 spdlog)来记录日志信息。

示例代码:
#include <iostream>
#include <thread>
#include <spdlog/spdlog.h>void threadFunction() {try {// 模拟可能抛出异常的代码throw std::runtime_error("An error occurred in the thread");} catch (const std::exception& e) {spdlog::error("Exception caught in thread: {}", e.what());// 进行其他必要的处理}
}int main() {auto logger = spdlog::stdout_color_mt("console");std::thread t(threadFunction);t.join();return 0;
}

6. 使用线程池

线程池可以更好地管理和复用线程,减少线程创建和销毁的开销。线程池通常会处理线程中的异常,并确保线程池的正常运行。

示例代码:
#include <iostream>
#include <thread>
#include <vector>
#include <queue>
#include <functional>
#include <mutex>
#include <condition_variable>class ThreadPool {
public:ThreadPool(size_t numThreads) : stop(false) {for (size_t i = 0; i < numThreads; ++i) {threads.emplace_back([this] {while (true) {std::function<void()> task;{std::unique_lock<std::mutex> lock(queueMutex);condition.wait(lock, [this] { return stop || !tasks.empty(); });if (stop && tasks.empty()) {return;}task = std::move(tasks.front());tasks.pop();}try {task();} catch (const std::exception& e) {std::cerr << "Exception caught in thread pool: " << e.what() << std::endl;}}});}}~ThreadPool() {{std::unique_lock<std::mutex> lock(queueMutex);stop = true;}condition.notify_all();for (std::thread& t : threads) {t.join();}}template <typename Func, typename... Args>auto enqueue(Func&& func, Args&&... args) -> std::future<decltype(func(args...))> {using return_type = decltype(func(args...));auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward<Func>(func), std::forward<Args>(args)...));std::future<return_type> res = task->get_future();{std::unique_lock<std::mutex> lock(queueMutex);if (stop) {throw std::runtime_error("Enqueue on stopped ThreadPool");}tasks.emplace([task]() { (*task)(); });}condition.notify_one();return res;}private:std::vector<std::thread> threads;std::queue<std::function<void()>> tasks;std::mutex queueMutex;std::condition_variable condition;bool stop;
};void simulateWork() {throw std::runtime_error("An error occurred in the task");
}int main() {ThreadPool pool(4);std::future<void> future = pool.enqueue(simulateWork);try {future.get();} catch (const std::exception& e) {std::cerr << "Exception caught in main: " << e.what() << std::endl;}return 0;
}

总结

在并发编程中,确保异常安全需要从多个方面着手,包括异常捕获和处理、资源管理、线程同步、异常传播、日志记录和使用线程池等。通过这些方法,可以有效地处理并发环境中的异常,提高程序的稳定性和可靠性。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/66482.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

各语言镜像配置汇总

镜像配置汇总 Nodejs [ npm ]Python [ pip ] Nodejs [ npm ] // # 记录日期&#xff1a;2025-01-20// 查询当前使用的镜像 npm get registry// 设置淘宝镜像 npm config set registry https://registry.npmmirror.com/// 恢复为官方镜像 npm config set registry https://regi…

Navicat Premium 数据可视化

工作区&#xff0c;数据源以及图表 数据可视化是使用可视化组件&#xff08;例如图表&#xff0c;图形和地图&#xff09;的信息和数据的图形表示。 数据可视化工具提供了一种可访问的方式&#xff0c;用于查看和理解数据中的趋势&#xff0c;异常值和其他模式。 在Navicat中&…

linux通过web向mac远程传输字符串,mac收到后在终端中直接打印。

要通过Web从Linux向Mac远程传输字符串&#xff0c;并在Mac的终端中直接打印&#xff0c;可以使用以下方法。这里假设Linux作为服务器&#xff0c;Mac作为客户端。 方法 1&#xff1a;使用Python的HTTP服务器 在Linux上启动一个简单的HTTP服务器&#xff0c;Mac通过curl获取字符…

【系统分享01】Python+Vue电影推荐系统

大家好&#xff0c;作为一名老程序员&#xff0c;今天我将带你一起走进电影推荐系统的世界&#xff0c;分享如何利用 Django REST Framework 和 Vue 搭建一套完整的电影推荐系统&#xff0c;结合 协同过滤算法&#xff0c;根据用户评分与影片喜好&#xff0c;精准推送用户可能喜…

Spring Boot+Vue

Spring BootVue 前后端分离是一种非常流行且高效的开发模式&#xff0c;以下是关于其相关方面的详细介绍&#xff1a; 前端&#xff08;Vue&#xff09;部分 • 项目搭建 • 使用 Vue CLI 创建项目&#xff0c;它提供了丰富的插件和配置选项&#xff0c;能够快速生成项目基础…

第十四章:计算机新技术

文章目录&#xff1a; 一&#xff1a;云计算 二&#xff1a;大数据 三&#xff1a;物联网 四&#xff1a;人工智能 五&#xff1a;移动网络与应用 六&#xff1a;电子商务 七&#xff1a;虚拟实现 八&#xff1a;区块链 一&#xff1a;云计算 概念云基于⽹络&#xff0…

【大数据2025】MapReduce

MapReduce 基础介绍 起源与发展&#xff1a;是 2004 年 10 月谷歌发表的 MAPREDUCE 论文的开源实现&#xff0c;最初用于大规模网页数据并行处理&#xff0c;现成为 Hadoop 核心子项目之一&#xff0c;是面向批处理的分布式计算框架。基本原理&#xff1a;分为 map 和 reduce …

主从复制

简述mysql 主从复制原理及其工作过程&#xff0c;配置一主两从并验证。 主从原理&#xff1a;MySQL 主从同步是一种数据库复制技术&#xff0c;它通过将主服务器上的数据更改复制到一个或多个从服务器&#xff0c;实现数据的自动同步。 主从同步的核心原理是将主服务器上的二…

【博客之星评选】2024年度前端学习总结

故事的开端...始于2024年第一篇前端技术博客 那故事的终末...也该结束于陪伴了我一整年的前端知识了 踏入 2025 年&#xff0c;满心激动与自豪&#xff0c;我成功闯进了《2024 年度 CSDN 博客之星总评选》的 TOP300。作为一名刚接触技术写作不久的萌新&#xff0c;这次能走到这…

Ubuntu 24.04 LTS 服务器折腾集

目录 Ubuntu 更改软件源Ubuntu 系统语言英文改中文windows 远程链接 Ubuntu 图形界面Windows 通过 openssh 连接 UbuntuUbuntu linux 文件权限Ubuntu 空闲硬盘挂载到 文件管理器的 other locationsUbuntu 开启 SMB 服务&#xff0c;并通过 windows 访问Ubuntu安装Tailscale&am…

《TikTok停服:信息安全警钟长鸣》

一、TikTok 停服事件回顾 2025 年 1 月 18 日晚&#xff0c;TikTok 通知美国用户&#xff0c;由于美官方禁令于 19 日起生效&#xff0c;TikTok 软件将暂时对用户停止服务。这一消息犹如一颗重磅炸弹&#xff0c;瞬间在全球范围内掀起轩然大波。美国用户对此猝不及防&#xff0…

1166 Summit (25)

A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of everyone. Now given a set of tenta…

图论DFS:黑红树

我的个人主页 {\large \mathsf{{\color{Red} 我的个人主页} } } 我的个人主页 往 {\color{Red} {\Huge 往} } 往 期 {\color{Green} {\Huge 期} } 期 文 {\color{Blue} {\Huge 文} } 文 章 {\color{Orange} {\Huge 章}} 章 DFS 算法&#xff1a;记忆化搜索DFS 算法&#xf…

C++,设计模式,【目录篇】

文章目录 1. 简介2. 设计模式的分类2.1 创建型模式&#xff08;Creational Patterns&#xff09;&#xff1a;2.2 结构型模式&#xff08;Structural Patterns&#xff09;&#xff1a;2.3 行为型模式&#xff08;Behavioral Patterns&#xff09;&#xff1a; 3. 使用设计模式…

掌握提示词工程:大模型使用入门指南

掌握提示词工程&#xff1a;大模型使用入门指南 近年来&#xff0c;大语言模型&#xff08;如 GPT、Claude 等&#xff09;的强大能力令人印象深刻&#xff0c;但要想充分发挥这些模型的潜力&#xff0c;仅仅依靠其预训练能力还不够。提示词工程&#xff08;Prompt Engineerin…

如何使用 useMemo 和 memo 优化 React 应用性能?

使用 useMemo 和 memo 优化 React 应用性能 在构建复杂的 React 应用时&#xff0c;性能优化是确保应用流畅运行的关键。React 提供了多种工具来帮助开发者优化组件的渲染和计算逻辑&#xff0c;其中 useMemo 和 memo 是两个非常有用的 Hook。本文将详细介绍这两个工具的使用方…

Agent Laboratory: Using LLM Agents as Research Assistants 论文简介

加速机器学习研究的智能实验室——Agent Laboratory 1. 引言 随着人工智能技术的飞速发展&#xff0c;机器学习领域正以前所未有的速度推进科学发现和技术创新。然而&#xff0c;传统的科学研究模式往往受到时间、资源和专业知识限制&#xff0c;阻碍了研究者们探索新想法的能…

【网络协议】【http】【https】ECDHE-TLS1.2

【网络协议】【http】【https】ECDHE-TLS1.2 ECDHE算法 1.客户端和服务器端事先确定好使用哪种椭圆曲线&#xff0c;和曲线上的基点G&#xff0c;这两个参数都是公开的&#xff0c; 双方各自随机生成一个随机数作为私钥d&#xff0c;并与基点 G相乘得到公钥Q(QdG)&#xff0c…

规避路由冲突

路由冲突是指在网络中存在两个或多个路由器在进行路由选择时出现矛盾&#xff0c;导致网络数据包无法正确传输&#xff0c;影响网络的正常运行。为了规避路由冲突&#xff0c;可以采取以下措施&#xff1a; 一、合理规划IP地址 分配唯一IP&#xff1a;确保每个设备在网络中都有…

项目实战--网页五子棋(游戏大厅)(3)

我们的游戏大厅界面主要需要包含两个功能&#xff0c;一是显示用户信息&#xff0c;二是匹配游戏按钮 1. 页面实现 hall.html <!DOCTYPE html> <html lang"ch"> <head><meta charset"UTF-8"><meta name"viewport"…