From the course: Mastering C++: Exception Handling
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Cross-thread exception propagation - C++ Tutorial
From the course: Mastering C++: Exception Handling
Cross-thread exception propagation
- [Instructor] In our previous video, each thread managed its own exceptions, preventing one thread's problem from affecting others. However, there may be situations where threads are spawned from a parent, and exceptions need to be communicated back to the parent thread. In such scenarios, we can use std::promise and std::future. Let's have a look at the following code. In the main function, we create a promise and obtain a future from it. This future will later be used to retrieve the exception thrown in the thread. We then start a new thread passing in the threadTask function and the reference to the promise. Then we wait for the thread to complete using join. Now let's inspect the threadTask function. This function is intended to run in a separate thread. It tries to execute some code that throws an exception. Instead of letting the exception propagate and terminate the program, since 100 exceptions in threads lead to std::terminate, we catch any exception and store it inside the…
Contents
-
-
-
-
-
(Locked)
Advanced error handling3m 29s
-
(Locked)
The evolution of exception specs4m 27s
-
(Locked)
Rogue exceptions4m 39s
-
(Locked)
Thread-local exceptions2m 34s
-
(Locked)
Cross-thread exception propagation2m 37s
-
(Locked)
Challenge: Catching rogue exceptions1m 30s
-
(Locked)
Solution: Catching rogue exceptions1m 45s
-
(Locked)
-
-