51#ifndef DOXYGEN_SHOULD_SKIP_THIS
59 template <
typename FUNCTION,
typename... ARGS >
63 exec_func(0, 1, std::forward< ARGS >(func_args)...);
65 if (nb_threads <= 1) {
66 exec_func(0, 1, std::forward< ARGS >(func_args)...);
74 std::vector< std::exception_ptr > func_exceptions(nb_threads,
nullptr);
77# pragma omp parallel num_threads(int(nb_threads))
80 const std::size_t this_thread = omp_get_thread_num();
83 exec_func(this_thread, nb_threads, std::forward< ARGS >(func_args)...);
84 }
catch (...) { func_exceptions[this_thread] = std::current_exception(); }
91 for (
const auto& exc: func_exceptions) {
92 if (exc !=
nullptr) { std::rethrow_exception(exc); }
99 template <
typename FUNC1,
typename FUNC2,
typename... ARGS >
103 ARGS&&... func_args) {
107 exec_func(0, 1, std::forward< ARGS >(func_args)...);
109 undo_func(0, 1, std::forward< ARGS >(func_args)...);
113 if (nb_threads <= 1) {
115 exec_func(0, 1, std::forward< ARGS >(func_args)...);
117 undo_func(0, 1, std::forward< ARGS >(func_args)...);
127 std::vector< std::exception_ptr > func_exceptions(nb_threads,
nullptr);
130# pragma omp parallel num_threads(int(nb_threads))
136 exec_func(this_thread, nb_threads, std::forward< ARGS >(func_args)...);
137 }
catch (...) { func_exceptions[this_thread] = std::current_exception(); }
141 bool exception_raised =
false;
142 for (
const auto& exc: func_exceptions) {
143 if (exc !=
nullptr) {
144 exception_raised =
true;
149 if (exception_raised) {
151 std::vector< std::exception_ptr > undo_func_exceptions(nb_threads,
nullptr);
154# pragma omp parallel num_threads(int(nb_threads))
160 undo_func(this_thread, nb_threads, std::forward< ARGS >(func_args)...);
161 }
catch (...) { undo_func_exceptions[this_thread] = std::current_exception(); }
168 for (
const auto& exc: func_exceptions) {
169 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.