OpenMP forbids code which leaves the openmp block via exception. Therefore I\'m looking for a nice way of getting the exceptions from an openmp block with the purpose of rethrow
You can utilize a few more C++11 tools to clean up the syntax a bit. Add this variadic member function to your ThreadException
class:
class ThreadException {
// ...
template <typename Function, typename... Parameters>
void Run(Function f, Parameters... params)
{
try
{
f(params...);
}
catch (...)
{
CaptureException();
}
}
};
Then when calling inside an OpenMP construct use a lambda function like so:
ThreadException e;
#pragma omp parallel for
for (int i = 0; i < n; i++)
{
e.Run([=]{
// code that might throw
// ...
});
}
e.Rethrow()