]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Drop TaskResult::wait_and_move_result() in favor of join().
authorWolfgang Bangerth <bangerth@colostate.edu>
Mon, 22 Jul 2024 15:17:00 +0000 (09:17 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Mon, 22 Jul 2024 15:40:48 +0000 (09:40 -0600)
include/deal.II/base/task_result.h

index c231e22a15ab16035f213ad7896af0b1835219fd..a0501eb7ab5f75c95c3f09fa975054e8230f4799 100644 (file)
@@ -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 <typename T>
   inline void
-  TaskResult<T>::join()
+  TaskResult<T>::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<T>::value() const
   {
     if (!result_is_available)
-      wait_and_move_result();
+      join();
     return task_result.value();
   }
-
-
-
-  template <typename T>
-  inline void
-  TaskResult<T>::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<std::mutex> 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
 
 /**

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.