Home > Backend Development > C++ > How do you propagate exceptions between threads in C using `exception_ptr`?

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

Linda Hamilton
Release: 2024-10-31 23:15:28
Original
612 people have browsed it

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

Propagating Exceptions Between Threads in C

The task of propagating exceptions between threads in C arises when a function called from a main thread spawns multiple worker threads for CPU-intensive work. The challenge lies in handling exceptions that may occur on the worker threads and propagating them back to the main thread for proper handling.

Conventional Approach

One common approach is to manually catch a variety of exceptions on worker threads, record their details, and rethrow them on the main thread. However, this method has a limitation in that it supports only a fixed set of exception types. Any new exception types introduced in the future would require manual modification to the code.

C 11 Exception Handling

C 11 introduces the exception_ptr type, providing a more robust solution for exception propagation. This type allows for the transportation of exceptions between threads.

Example Implementation

The following example demonstrates how to propagate exceptions using exception_ptr:

<code class="cpp">#include <iostream>
#include <thread>
#include <exception>
#include <stdexcept>

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 << "Worker thread exited with exception: " << ex.what() << "\n";
    }
  }

  return 0;
}</code>
Copy after login

In this example, the worker thread catches any exception that occurs and assigns it to eptr. On the main thread, the eptr is checked and, if an exception is present, it is rethrown.

Note for Multiple Worker Threads

If you have multiple worker threads, you will need to maintain separate exception_ptr instances for each thread to capture any potential exceptions.

Additional Considerations

  • exception_ptr is a shared pointer-like type, so it is crucial to ensure that at least one exception_ptr is pointing to each exception to prevent them from being released.
  • Microsoft-specific: When using SEH exceptions with the /EHa compiler flag, the example code may also capture SEH exceptions like access violations. This may not be desirable in all cases.

The above is the detailed content of How do you propagate exceptions between threads in C using `exception_ptr`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template