52#ifndef DOXYGEN_SHOULD_SKIP_THIS
60 template <
typename FUNCTION,
typename... ARGS >
64 exec_func(0, 1, std::forward< ARGS >(func_args)...);
66 if (nb_threads <= 1) {
67 exec_func(0, 1, std::forward< ARGS >(func_args)...);
70 std::vector< std::exception_ptr > func_exceptions(nb_threads,
nullptr);
76# pragma omp parallel num_threads(int(nb_threads))
79 const std::size_t this_thread = omp_get_thread_num();
82 exec_func(this_thread, nb_threads, func_args...);
83 }
catch (...) { func_exceptions[this_thread] = std::current_exception(); }
90 for (
const auto& exc: func_exceptions) {
91 if (exc !=
nullptr) { std::rethrow_exception(exc); }
98 template <
typename FUNC1,
typename FUNC2,
typename... ARGS >
102 ARGS&&... func_args) {
106 exec_func(0, 1, std::forward< ARGS >(func_args)...);
108 undo_func(0, 1, std::forward< ARGS >(func_args)...);
112 if (nb_threads <= 1) {
114 exec_func(0, 1, std::forward< ARGS >(func_args)...);
116 undo_func(0, 1, std::forward< ARGS >(func_args)...);
126 std::vector< std::exception_ptr > func_exceptions(nb_threads,
nullptr);
129# pragma omp parallel num_threads(int(nb_threads))
135 exec_func(this_thread, nb_threads, func_args...);
136 }
catch (...) { func_exceptions[this_thread] = std::current_exception(); }
140 bool exception_raised =
false;
141 for (
const auto& exc: func_exceptions) {
142 if (exc !=
nullptr) {
143 exception_raised =
true;
148 if (exception_raised) {
150 std::vector< std::exception_ptr > undo_func_exceptions(nb_threads,
nullptr);
153# pragma omp parallel num_threads(int(nb_threads))
159 undo_func(this_thread, nb_threads, func_args...);
160 }
catch (...) { undo_func_exceptions[this_thread] = std::current_exception(); }
167 for (
const auto& exc: func_exceptions) {
168 if (exc !=
nullptr) { std::rethrow_exception(exc); }
static std::atomic< int > nbRunningThreadsExecutors_
he number of currently running ThreadExecutors
unsigned int getThreadNumber()
Get the calling thread id.
gum is the global namespace for all aGrUM entities
static void execute(std::size_t nb_threads, FUNCTION exec_func, ARGS &&... func_args)
executes a function using several threads
static void executeOrUndo(std::size_t nb_threads, FUNC1 exec_func, FUNC2 undo_func, ARGS &&... func_args)
executes in parallel a function and undoes it if execptions are raised
A class to execute several threads by exploiting openMP.