aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
threadExecutorSTL_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
49#include <algorithm>
50#include <exception>
51#include <functional>
52#include <thread>
53#include <tuple>
54#include <vector>
55
57
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
59
60
61namespace gum {
62
63 namespace threadsSTL {
64
66 template < typename FUNCTION, typename... ARGS >
67 void ThreadExecutor::execute(std::size_t nb_threads, FUNCTION exec_func, ARGS&&... func_args) {
68 if (nb_threads <= 1) {
69 exec_func(0, 1, std::forward< ARGS >(func_args)...);
70 } else {
71 // indicate that we start a new threadExecutor
73
74 // here, we shall create the threads, but also one std::exception_ptr
75 // for each thread. This will allow us to catch the exception raised
76 // by the threads
77 std::vector< std::thread > threads;
78 threads.reserve(nb_threads);
79 std::vector< std::exception_ptr > func_exceptions(nb_threads, nullptr);
80
81 // create a lambda that will execute exec_func while catching its exceptions
82 auto real_exec_func = [&exec_func, nb_threads](std::size_t this_thread,
83 std::exception_ptr& exc,
84 ARGS&... args) -> void {
85 try {
86 exec_func(this_thread, nb_threads, args...);
87 } catch (...) { exc = std::current_exception(); }
88 };
89
90 // launch the threads
91 for (std::size_t i = std::size_t(0); i < nb_threads; ++i) {
92 threads.push_back(
93 std::thread(real_exec_func, i, std::ref(func_exceptions[i]), std::ref(func_args)...));
94 // std::ref(std::forward< ARGS >(func_args))...));
95 }
96
97 // wait for the threads to complete their executions
98 std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
99
100 // now, we have completed the execution of the ThreadExecutor
102
103 // now, check if one exception has been raised
104 for (const auto& exc: func_exceptions) {
105 if (exc != nullptr) { std::rethrow_exception(exc); }
106 }
107 }
108 }
109
111 template < typename FUNC1, typename FUNC2, typename... ARGS >
112 void ThreadExecutor::executeOrUndo(std::size_t nb_threads,
113 FUNC1 exec_func,
114 FUNC2 undo_func,
115 ARGS&&... func_args) {
116 if (nb_threads <= 1) {
117 try {
118 exec_func(0, 1, std::forward< ARGS >(func_args)...);
119 } catch (...) {
120 undo_func(0, 1, std::forward< ARGS >(func_args)...);
121 throw;
122 }
123 } else {
124 // indicate that we start a new threadExecutor
126
127 // here, we shall create the threads, but also one std::exception_ptr
128 // for each thread. This will allow us to catch the exception raised
129 // by the threads
130 std::vector< std::thread > threads;
131 threads.reserve(nb_threads);
132 std::vector< std::exception_ptr > func_exceptions(nb_threads, nullptr);
133
134 // create a lambda that will execute exec_func while catching its exceptions
135 auto real_exec_func = [&exec_func, nb_threads](std::size_t this_thread,
136 std::exception_ptr& exc,
137 ARGS&... args) -> void {
138 try {
139 exec_func(this_thread, nb_threads, std::forward< ARGS >(args)...);
140 } catch (...) { exc = std::current_exception(); }
141 };
142
143
144 // launch the threads
145 for (std::size_t i = std::size_t(0); i < nb_threads; ++i) {
146 threads.push_back(
147 std::thread(real_exec_func, i, std::ref(func_exceptions[i]), std::ref(func_args)...));
148 // std::ref(std::forward< ARGS >(func_args))...));
149 }
150
151 // wait for the threads to complete their executions
152 std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
153
154 // now, check if one exception has been raised
155 bool exception_raised = false;
156 for (const auto& exc: func_exceptions) {
157 if (exc != nullptr) {
158 exception_raised = true;
159 break;
160 }
161 }
162
163
164 if (exception_raised) {
165 // create a lambda that will execute undo_func while catching
166 // its exceptions
167 auto real_undo_func = [&undo_func, nb_threads](std::size_t this_thread,
168 std::exception_ptr& exc,
169 ARGS&... args) -> void {
170 try {
171 undo_func(this_thread, nb_threads, args...);
172 } catch (...) { exc = std::current_exception(); }
173 };
174
175 // launch the repair threads
176 threads.clear();
177 std::vector< std::exception_ptr > undo_func_exceptions(nb_threads, nullptr);
178 for (std::size_t i = std::size_t(0); i < nb_threads; ++i) {
179 // we just need to repair the threads that did not raise exceptions
180 if (func_exceptions[i] == nullptr)
181 threads.push_back(std::thread(real_undo_func,
182 i,
183 std::ref(undo_func_exceptions[i]),
184 std::ref(func_args)...));
185 // std::ref(std::forward< ARGS >(func_args))...));
186 }
187
188 // wait for the threads to complete their executions
189 std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
190
191 // now, we have completed the execution of the ThreadExecutor
193
194 // rethrow the exception
195 for (const auto& exc: func_exceptions) {
196 if (exc != nullptr) { std::rethrow_exception(exc); }
197 }
198 } else {
199 // now, we have completed the execution of the ThreadExecutor
201 }
202 }
203 }
204
205
206 } /* namespace threadsSTL */
207
208} /* namespace gum */
209
210#endif /* DOXYGEN_SHOULD_SKIP_THIS */
static std::atomic< int > nbRunningThreadsExecutors_
he number of currently running ThreadExecutors
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
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 std::thread.