[PATCH v2 11/32] Return vector of results from parallel_for_each

Lancelot SIX lsix@lancelotsix.com
Wed Nov 17 20:37:55 GMT 2021


> +
> +/* See the generic template.  */
> +template<>
> +struct par_for_accumulator<void>
> +{
> +public:
> +
> +  explicit par_for_accumulator (size_t n_threads)
> +    : m_futures (n_threads)
> +  {
> +  }
> +
> +  /* This specialization does not compute results.  */
> +  typedef void result_type;
> +
> +  void post (size_t i, std::function<void ()> task)
> +  {
> +    m_futures[i]
> +      = gdb::thread_pool::g_thread_pool->post_task (std::move (task));
> +  }
> +
> +  result_type finish (std::function<void ()> task)
> +  {
> +    task ();
> +
> +    for (auto &future : m_futures)
> +      future.wait ();

Hi,

There is a point here I missed when I read though the V1 of the series.

I think you also want to use 'get' here instead of 'wait' (as you do in
the non specialized version of the template).

Wait 'just' makes sure the result becomes accessible (in this case,
makes sure the task is executed), while get also accesses it.  Even if
the task returns nothing, if an exception is thrown during execution of
the task, calling wait will just make sure the exception is stored in
the future object.  Calling get will re-throw the exception, which is
probably what we want.

Best,
Lancelot.

> +  }
> +
> +private:
> +
> +  std::vector<std::future<void>> m_futures;
> +};
> +
> +}
> +


More information about the Gdb-patches mailing list