]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Enable writing Threads::Thread<T> for types T that are only movable.
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 29 Mar 2018 01:30:28 +0000 (19:30 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Thu, 29 Mar 2018 01:30:28 +0000 (19:30 -0600)
Previously, it was not possible to write Threads::new_thread(&foo) or Threads::new_task(&foo)
for functions that return an object whose type is not copyable, but is movable. That's
because in a number of places in the internal machinery of the Threads::Thread and
Threads::Task classes, we didn't account for such types.

This patch fixes this.

include/deal.II/base/thread_management.h

index 006f89fdbb2ef5cfa0e9bef4d01c085404116106..ed332ba1bdd015579046dfefe87c27f645b2ac91 100644 (file)
@@ -692,16 +692,18 @@ namespace Threads
     private:
       RT value;
     public:
+      typedef RT &reference_type;
+
       inline return_value () : value() {}
 
-      inline RT get () const
+      inline reference_type get ()
       {
         return value;
       }
 
-      inline void set (RT v)
+      inline void set (RT &&v)
       {
-        value = v;
+        value = std::move(v);
       }
     };
 
@@ -720,14 +722,17 @@ namespace Threads
     private:
       RT *value;
     public:
+      typedef RT &reference_type;
+
       inline return_value ()
         : value(nullptr)
       {}
 
-      inline RT &get () const
+      inline reference_type get () const
       {
         return *value;
       }
+
       inline void set (RT &v)
       {
         value = &v;
@@ -745,6 +750,8 @@ namespace Threads
      */
     template <> struct return_value<void>
     {
+      typedef void reference_type;
+
       static inline void get () {}
     };
   }
@@ -1025,7 +1032,8 @@ namespace Threads
      * Get the return value of the function of the thread. Since this is only
      * available once the thread finishes, this implicitly also calls join().
      */
-    RT return_value ()
+    typename internal::return_value<RT>::reference_type
+    return_value ()
     {
       join ();
       return thread_descriptor->ret_val->get();
@@ -1702,7 +1710,7 @@ namespace Threads
      * constructor of this class and have not assigned a task object to it. In
      * other words, the function joinable() must return true.
      */
-    RT return_value ()
+    typename internal::return_value<RT>::reference_type return_value ()
     {
       join ();
       return task_descriptor->ret_val.get();

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.