Table of Contents
How to handle cross-thread C++ exceptions
Preface
Exception delivery mechanism
Practical case
Conclusion
Home Backend Development C++ How to handle cross-thread C++ exceptions?

How to handle cross-thread C++ exceptions?

Jun 06, 2024 am 10:44 AM
c++ Exception handling

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();
Copy after login

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));
}
Copy after login

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

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

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;
}
Copy after login

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C++ object layout is aligned with memory to optimize memory usage efficiency C++ object layout is aligned with memory to optimize memory usage efficiency Jun 05, 2024 pm 01:02 PM

C++ object layout is aligned with memory to optimize memory usage efficiency

How to implement a custom comparator in C++ STL? How to implement a custom comparator in C++ STL? Jun 05, 2024 am 11:50 AM

How to implement a custom comparator in C++ STL?

How to implement the Strategy Design Pattern in C++? How to implement the Strategy Design Pattern in C++? Jun 06, 2024 pm 04:16 PM

How to implement the Strategy Design Pattern in C++?

Similarities and Differences between Golang and C++ Similarities and Differences between Golang and C++ Jun 05, 2024 pm 06:12 PM

Similarities and Differences between Golang and C++

How to copy a C++ STL container? How to copy a C++ STL container? Jun 05, 2024 am 11:51 AM

How to copy a C++ STL container?

What are the underlying implementation principles of C++ smart pointers? What are the underlying implementation principles of C++ smart pointers? Jun 05, 2024 pm 01:17 PM

What are the underlying implementation principles of C++ smart pointers?

How to implement C++ multi-thread programming based on the Actor model? How to implement C++ multi-thread programming based on the Actor model? Jun 05, 2024 am 11:49 AM

How to implement C++ multi-thread programming based on the Actor model?

How does C++ exception handling support custom error handling routines? How does C++ exception handling support custom error handling routines? Jun 05, 2024 pm 12:13 PM

How does C++ exception handling support custom error handling routines?

See all articles