]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Introduce TaskResult::empty().
authorWolfgang Bangerth <bangerth@colostate.edu>
Sun, 21 Jul 2024 23:22:22 +0000 (17:22 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 23 Jul 2024 03:39:10 +0000 (21:39 -0600)
include/deal.II/base/task_result.h

index 478a3a8fc0758246e594fe81e9c1d80b1d336793..abbc6f8ade19ddf995cdde1dc76fd842f333423b 100644 (file)
@@ -337,6 +337,15 @@ namespace Threads
     const T &
     value() const;
 
+    /**
+     * Return whether the object has an associated task result object or not.
+     * Only objects that are default-initialized, or for which clear() has
+     * been called, or that have been moved from, will return `true` in
+     * this function.
+     */
+    bool
+    empty() const;
+
   private:
     /**
      * An atomic flag that allows us to test whether the task has finished
@@ -504,6 +513,35 @@ namespace Threads
 
 
 
+  template <typename T>
+  inline bool
+  TaskResult<T>::empty() const
+  {
+    // If we have waited before, then return immediately:
+    if (result_is_available)
+      return false;
+    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 false;
+        else
+          // If there is a task, then this object is not empty. Otherwise, we
+          // have no result and no task, so the object is empty:
+          if (task.has_value())
+            return false;
+          else
+            return true;
+      }
+  }
+
+
+
   template <typename T>
   inline const T &
   TaskResult<T>::value() const

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.