Celeritas  0.5.0-56+6b053cd
Public Member Functions | List of all members
celeritas::MultiExceptionHandler Class Reference

Temporarily store exception pointers. More...

#include <MultiExceptionHandler.hh>

Public Types

Type aliases
using VecExceptionPtr = std::vector< std::exception_ptr >
 

Public Member Functions

 CELER_DEFAULT_COPY_MOVE (MultiExceptionHandler)
 
 ~MultiExceptionHandler ()
 Terminate if destroyed without handling exceptions.
 
void operator() (std::exception_ptr p)
 Thread-safe capture of the given exception.
 
bool empty () const
 Whether no exceptions have been stored (not thread safe)
 
VecExceptionPtr release ()
 Release exceptions for someone else to process (not thread safe)
 

Detailed Description

Temporarily store exception pointers.

This is useful for storing multiple exceptions in unrelated loops (where one exception shouldn't affect the program flow outside of the scope), especially for OpenMP parallel execution, where exceptions cannot be propagated.

MultiExceptionHandler capture_exception;
#pragma omp parallel for
for (size_type i = 0; i < data.states.size(); ++i)
{
CELER_TRY_HANDLE(step(TrackSlotId{i}), capture_exception);
}
log_and_rethrow(std::move(capture_exception));
#define CELER_TRY_HANDLE(STATEMENT, HANDLE_EXCEPTION)
"Try" to execute the statement, and "handle" all thrown errors by calling the given function-like err...
Definition: Macros.hh:186
void log_and_rethrow(MultiExceptionHandler &&exceptions)
Throw the first exception and log all the rest.
Definition: MultiExceptionHandler.hh:88
Note
This class implements an OpenMP critical mutex, not a std mutex. If using this class in a std::thread context, wrap the call operator in a lambda with a std::scoped_lock . We could refactor as a CRTP class with a protected push_back function that lets us specialize the mutex implementation.

The documentation for this class was generated from the following files: