From 868d78b1923f7b80ebd64e2054608cac640c0c2f Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Mon, 26 Jun 2023 22:48:55 +0100 Subject: [PATCH 1/3] change `return last` to `return` * "Void function 'parallel_partial_sum>' should not return a value" * example from book is actually simple `return` --- listings/listing_8.11.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listings/listing_8.11.cpp b/listings/listing_8.11.cpp index 5ea88cc..7f53915 100644 --- a/listings/listing_8.11.cpp +++ b/listings/listing_8.11.cpp @@ -57,7 +57,7 @@ void parallel_partial_sum(Iterator first,Iterator last) unsigned long const length=std::distance(first,last); if(!length) - return last; + return; unsigned long const min_per_thread=25; unsigned long const max_threads= From 6d62a767145e958937feea70df15b44afe58915b Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Mon, 26 Jun 2023 23:21:04 +0100 Subject: [PATCH 2/3] `value_type&` -> `value_type const&` * "Non-const lvalue reference to type 'value_type' (aka 'int') cannot bind to a temporary of type 'int'" * simple fix making the lvalue reference const --- listings/listing_8.11.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listings/listing_8.11.cpp b/listings/listing_8.11.cpp index 7f53915..75709c3 100644 --- a/listings/listing_8.11.cpp +++ b/listings/listing_8.11.cpp @@ -24,7 +24,7 @@ void parallel_partial_sum(Iterator first,Iterator last) std::partial_sum(begin,end,begin); if(previous_end_value) { - value_type& addend=previous_end_value->get(); + value_type const& addend=previous_end_value->get(); *last+=addend; if(end_value) { From 5fd95a34d155cbce3f06587a5410fff27217f11a Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Mon, 26 Jun 2023 23:29:18 +0100 Subject: [PATCH 3/3] complete `join_threads` class * "Thread 1: signal SIGABRT" * code will crash with current implementation --- listings/listing_8.11.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/listings/listing_8.11.cpp b/listings/listing_8.11.cpp index 75709c3..3cd4b12 100644 --- a/listings/listing_8.11.cpp +++ b/listings/listing_8.11.cpp @@ -1,10 +1,20 @@ #include #include #include -struct join_threads -{ - join_threads(std::vector&) - {} + +class join_threads { +public: + join_threads(std::vector &threads) : threads_(threads) { } + + ~join_threads() + { + for (auto &t : threads_) { + if (t.joinable()) { t.join(); } + } + } + +private: + std::vector &threads_; }; template