How to handle cross-thread C++ exceptions?
Jun 06, 2024 am 10:44 AMIn 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
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

Similarities and Differences between Golang and C++

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

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

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