From: Wolfgang Bangerth Date: Thu, 29 Mar 2018 01:30:28 +0000 (-0600) Subject: Enable writing Threads::Thread for types T that are only movable. X-Git-Tag: v9.0.0-rc1~259^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5e100ff128fe5baf01a88e62b28208b105561116;p=dealii.git Enable writing Threads::Thread for types T that are only movable. 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. --- diff --git a/include/deal.II/base/thread_management.h b/include/deal.II/base/thread_management.h index 006f89fdbb..ed332ba1bd 100644 --- a/include/deal.II/base/thread_management.h +++ b/include/deal.II/base/thread_management.h @@ -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 { + 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::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::reference_type return_value () { join (); return task_descriptor->ret_val.get();