”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 如何使用“exception_ptr”在 C++ 线程之间传播异常?

如何使用“exception_ptr”在 C++ 线程之间传播异常?

发布于2024-11-06
浏览:521

How do you propagate exceptions between threads in C   using `exception_ptr`?

在 C 中的线程之间传播异常

当从主线程调用的函数生成多个线程时,就会出现在 C 中的线程之间传播异常的任务用于 CPU 密集型工作的工作线程。挑战在于处理工作线程上可能发生的异常并将其传播回主线程​​以进行正确处理。

传统方法

一种常见方法是手动捕获工作线程上的各种异常,记录它们的详细信息,然后在主线程上重新抛出它们。但是,此方法有一个限制,即它仅支持一组固定的异常类型。将来引入的任何新异常类型都需要手动修改代码。

C 11 异常处理

C 11 引入了 exception_ptr 类型,提供了更强大的解决方案用于异常传播。此类型允许在线程之间传输异常。

示例实现

以下示例演示如何使用Exception_ptr传播异常:

#include 
#include 
#include 
#include 

static std::exception_ptr eptr;

void worker() {
  try {
    // Simulated CPU-intensive work with a delay
    std::this_thread::sleep_for(std::chrono::seconds(1));
    throw std::runtime_error("Exception in worker thread");
  } catch (...) {
    eptr = std::current_exception();
  }
}

int main() {
  // Create a worker thread
  std::thread workerThread(worker);
  workerThread.join();

  // Check if an exception occurred on the worker thread
  if (eptr) {
    try {
      // Rethrow the exception on the main thread
      std::rethrow_exception(eptr);
    } catch (const std::exception &ex) {
      // Handle the exception on the main thread
      std::cerr 

在此示例中,工作线程捕获发生的任何异常并将其分配给 eptr。在主线程上,检查 eptr,如果存在异常,则重新抛出异常。

多工作线程注意事项

如果有多个工作线程,您需要为每个线程维护单独的Exception_ptr实例以捕获任何潜在的异常。

其他注意事项

  • exception_ptr是一个共享的类似指针的类型,因此,确保至少有一个 exception_ptr 指向每个异常以防止它们被释放至关重要。
  • Microsoft 特定:当使用带有 /EHa 编译器标志的 SEH 异常时,示例代码也可能捕获SEH 异常,例如访问冲突。这可能并不适合所有情况。
最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3