diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2020-08-01 11:11:41 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-01 11:11:41 -0400 |
| commit | 592fa7310492392440ee73f4cc0f962c66d5aba7 (patch) | |
| tree | 82d6fd20140acd8bbd9cbd9a953ac3c18019111e /src/thread_utils.h | |
| parent | 9d3281fd58e41f4320f36710a1a36f9ece1efdc6 (diff) | |
| parent | 64ba6cae1e6b74929d88de628bb2915cb9c6f2d2 (diff) | |
Merge pull request #1176 from isanae/2.3.1-fixes
2.3.1 fixes
Diffstat (limited to 'src/thread_utils.h')
| -rw-r--r-- | src/thread_utils.h | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/thread_utils.h b/src/thread_utils.h index f0067b6a..607d73a9 100644 --- a/src/thread_utils.h +++ b/src/thread_utils.h @@ -1,12 +1,31 @@ #ifndef MO2_THREAD_UTILS_H #define MO2_THREAD_UTILS_H +#include <log.h> #include <functional> #include <mutex> #include <thread> +// in main.cpp +void setUnhandledExceptionHandler(); +LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS *exceptionPtrs); + + namespace MOShared { +// starts an std::thread with an unhandled exception handler for core dumps +// and a top-level catch +// +template <class F> +std::thread startSafeThread(F&& f) +{ + return std::thread([f=std::forward<F>(f)] { + setUnhandledExceptionHandler(); + f(); + }); +} + + /** * Class that can be used to perform thread-safe memoization. * @@ -26,7 +45,7 @@ struct MemoizedLocked { template <class Callable> MemoizedLocked(Callable &&callable, T value = {}) : m_Fn{ std::forward<Callable>(callable) }, m_Value{ std::move(value) } { } - + template <class... Args> T& value(Args&&... args) const { if (m_NeedUpdating) { @@ -66,7 +85,7 @@ private: * */ template <class It, class Callable> -void parallelMap(It begin, It end, Callable callable, std::size_t nThreads) +void parallelMap(It begin, It end, Callable callable, std::size_t nThreads) { std::mutex m; std::vector<std::thread> threads(nThreads); @@ -75,7 +94,7 @@ void parallelMap(It begin, It end, Callable callable, std::size_t nThreads) // - The mutex is only used to fetch/increment the iterator. // - The callable is copied in each thread to avoid conflicts. for (auto &thread: threads) { - thread = std::thread([&m, &begin, end, callable]() { + thread = startSafeThread([&m, &begin, end, callable]() { while (true) { decltype(begin) it; { |
