Skip to content
Snippets Groups Projects
Commit cb5cf798 authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

Add boolean conversion to ThreadException

This slightly simplifies code avoiding extra boolean variable.
parent 5cf9379c
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,11 @@
/// Initial code from
/// https://stackoverflow.com/questions/11828539/elegant-exception-handling-in-openmp
///
/// The implementation does not guarantee, that while the exception is being
/// checked for nullptr in the bool-conversion operator, the capture is not
/// changing the pointer. This might lead to an exception being lost if there
/// are two exceptions thrown simultaneously.
class ThreadException
{
public:
......@@ -31,6 +36,8 @@ public:
}
}
explicit operator bool() const noexcept { return exception_ != nullptr; }
private:
std::exception_ptr exception_ = nullptr;
std::mutex lock_;
......
......@@ -136,7 +136,6 @@ void ParallelVectorMatrixAssembler::assembleWithJacobian(
ConcurrentMatrixView Jac_view(Jac);
ThreadException exception;
bool assembly_error = false;
#pragma omp parallel num_threads(num_threads_)
{
#ifdef _OPENMP
......@@ -172,7 +171,7 @@ void ParallelVectorMatrixAssembler::assembleWithJacobian(
for (std::ptrdiff_t element_id = 0; element_id < n_loc_asm;
++element_id)
{
if (assembly_error)
if (exception)
{
continue;
}
......@@ -187,7 +186,6 @@ void ParallelVectorMatrixAssembler::assembleWithJacobian(
catch (...)
{
exception.capture();
assembly_error = true;
continue;
}
......@@ -206,7 +204,7 @@ void ParallelVectorMatrixAssembler::assembleWithJacobian(
#pragma omp for nowait
for (std::ptrdiff_t i = 0; i < n_act_elem; ++i)
{
if (assembly_error)
if (exception)
{
continue;
}
......@@ -223,7 +221,6 @@ void ParallelVectorMatrixAssembler::assembleWithJacobian(
catch (...)
{
exception.capture();
assembly_error = true;
continue;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment