From ffd13182798adba9614df52f9555dd3f3d8449a3 Mon Sep 17 00:00:00 2001 From: Paul T Date: Thu, 1 Jun 2023 16:38:38 -0400 Subject: [PATCH] Add support for `std::future` with enqueue Added support for `std::future` return type with `dp::thread_pool::enqueue`. Fixes #37 --- include/thread_pool/thread_pool.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/include/thread_pool/thread_pool.h b/include/thread_pool/thread_pool.h index 0a5229b..2062151 100644 --- a/include/thread_pool/thread_pool.h +++ b/include/thread_pool/thread_pool.h @@ -117,7 +117,12 @@ namespace dp { auto task = [func = std::move(f), ... largs = std::move(args), promise = std::move(promise)]() mutable { try { - promise.set_value(func(largs...)); + if constexpr (std::is_same_v) { + func(largs...); + promise.set_value(); + } else { + promise.set_value(func(largs...)); + } } catch (...) { promise.set_exception(std::current_exception()); } @@ -139,7 +144,13 @@ namespace dp { auto task = [func = std::move(f), ... largs = std::move(args), promise = shared_promise]() { try { - promise->set_value(func(largs...)); + if constexpr (std::is_same_v) { + func(largs...); + promise->set_value(); + } else { + promise->set_value(func(largs...)); + } + } catch (...) { promise->set_exception(std::current_exception()); }