diff --git a/listings/listing_8.11.cpp b/listings/listing_8.11.cpp index 5ea88cc..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 @@ -24,7 +34,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) { @@ -57,7 +67,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=