Home  >  Article  >  Backend Development  >  How to handle cross-thread C++ exceptions?

How to handle cross-thread C++ exceptions?

WBOY
WBOYOriginal
2024-06-06 10:44:58730browse

In multi-threaded C++, exception handling is implemented through the std::promise and std::future mechanisms: use the promise object to record the exception in the thread that throws the exception. Use a future object to check for exceptions in the thread that receives the exception. Practical examples show how to use promises and futures to catch and handle exceptions in different threads.

How to handle cross-thread C++ exceptions?

How to handle cross-thread C++ exceptions

Preface

In multi-threaded programming, exceptions may be thrown in any thread . Handling exceptions across threads requires additional considerations because there is no explicit control over how and where exceptions are thrown.

Exception delivery mechanism

The C++ standard library provides a mechanism for delivering exceptions, called std::promise and std::future . We can use them to safely pass exceptions between threads.

std::promise is responsible for generating exceptions, while std::future is responsible for receiving exceptions. Both objects must be created in the same thread:

// 在主线程创建
std::promise<void> promise;
std::future<void> future = promise.get_future();

When the exception is thrown in other threads, we can use the promise object to pass it:

// 在 worker 线程
try {
  // ... 代码可能会抛出异常
}
catch (const std::exception& e) {
  promise.set_exception(std::make_exception_ptr(e));
}

The future object can then be used in the main thread to check for exceptions:

// 在主线程
try {
  future.get();
}
catch (const std::exception& e) {
  // 处理异常
}

Practical case

The following code shows how to use std::promise and std::future to handle cross-thread exceptions:

#include <iostream>
#include <future>
#include <thread>

// 打印函数以展示在不同线程中抛出的异常
void printFunction() {
  try {
    throw std::runtime_error("这是一个运行时错误!");
  }
  catch (const std::exception& e) {
    std::cerr << "Worker 线程捕获异常:" << e.what() << '\n';
  }
}

int main() {
  std::promise<void> promise;
  std::future<void> future = promise.get_future();

  // 在新线程中运行打印函数
  std::thread worker(printFunction);

  // 让主线程等待 worker 线程
  try {
    future.get();
  }
  catch (const std::exception& e) {
    std::cerr << "主线程捕获异常:" << e.what() << '\n';
  }

  worker.join();
  return 0;
}

Conclusion

By using std::promise and std: :future, we can handle cross-thread exceptions safely. This allows us to continue execution after an exception occurs and handle it later.

The above is the detailed content of How to handle cross-thread C++ exceptions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn