From: Wolfgang Bangerth Date: Mon, 22 Jul 2024 15:17:00 +0000 (-0600) Subject: Drop TaskResult::wait_and_move_result() in favor of join(). X-Git-Tag: v9.6.0-rc1~22^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d76ce54524f8aed54ef1fda2d038041937b45bb9;p=dealii.git Drop TaskResult::wait_and_move_result() in favor of join(). --- diff --git a/include/deal.II/base/task_result.h b/include/deal.II/base/task_result.h index c231e22a15..a0501eb7ab 100644 --- a/include/deal.II/base/task_result.h +++ b/include/deal.II/base/task_result.h @@ -320,7 +320,7 @@ namespace Threads * it isn't associated with a task, just return. */ void - join(); + join() const; /** * Return a reference to the object computed by the task. This @@ -356,14 +356,6 @@ namespace Threads * A lock object that guards access to all of the `mutable` objects above. */ mutable std::mutex mutex; - - /** - * Wait for the task to finish, move its result into the `task_result` - * object, and then release all information still associated with the - * task that originally computed the result. - */ - void - wait_and_move_result() const; }; @@ -487,7 +479,7 @@ namespace Threads template inline void - TaskResult::join() + TaskResult::join() const { // If we have waited before, then return immediately: if (result_is_available) @@ -503,11 +495,17 @@ namespace Threads if (result_is_available) return; else - // If there is a task, wait for it to finish. We could then move - // the result, but it's fine to postpone that until someone actually - // asks for the result. - if (task.has_value()) + { + // The object is not empty and it has not received its result yet. + // So it must have a task object: + Assert(task.has_value(), ExcInternalError()); + task.value().join(); + task_result = std::move(task.value().return_value()); + task.reset(); + + result_is_available = true; + } } } @@ -518,44 +516,9 @@ namespace Threads TaskResult::value() const { if (!result_is_available) - wait_and_move_result(); + join(); return task_result.value(); } - - - - template - inline void - TaskResult::wait_and_move_result() const - { - // If we have waited before, then return immediately: - if (result_is_available) - return; - else - // If we have not waited, wait now. We need to use the double-checking - // pattern to ensure that if two threads get to this place at the same - // time, one returns right away while the other does the work. Note - // that this happens under the lock, so only one thread gets to be in - // this code block at the same time: - { - std::lock_guard lock(mutex); - - if (result_is_available) - return; - else - { - Assert(task.has_value(), - ExcMessage("You cannot wait for the result of a TaskResult " - "object that has no task associated with it.")); - task.value().join(); - task_result = std::move(task.value().return_value()); - task.reset(); - - result_is_available = true; - } - } - } - } // namespace Threads /**