Commit c00a1691 authored by Simon Spannagel's avatar Simon Spannagel
Browse files

ThreadPool: execute init and finalize methods of worker directly in case of single-threaded running

parent 5602a16e
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -47,6 +47,14 @@ ThreadPool::ThreadPool(unsigned int num_threads,
                                  worker_init_function,
                                  worker_finalize_function);
        }

        // When running single-threadedly, execute initialize function directly and store finalize function for later
        if(threads_.empty()) {
            if(worker_init_function) {
                worker_init_function();
            }
            finalize_function_ = worker_finalize_function;
        }
    } catch(...) {
        destroy();
        throw;
@@ -149,6 +157,11 @@ void ThreadPool::destroy() {
            thread.join();
        }
    }

    // Execute the cleanup function at the end of run if running single-threaded
    if(threads_.empty() && finalize_function_) {
        finalize_function_();
    }
}

bool ThreadPool::valid() {
+1 −0
Original line number Diff line number Diff line
@@ -270,6 +270,7 @@ namespace allpix {
        using Task = std::unique_ptr<std::packaged_task<void()>>;
        SafeQueue<Task> queue_;
        bool with_buffered_{true};
        std::function<void()> finalize_function_{};

        std::atomic_bool done_{false};