]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
libstdc++: Fix for explicit copy ctors in <thread> and <future> [PR106695]
authorJonathan Wakely <jwakely@redhat.com>
Mon, 22 Aug 2022 14:42:17 +0000 (15:42 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 7 Sep 2022 13:01:23 +0000 (14:01 +0100)
When I changed std::thread and std::async to avoid unnecessary move
construction of temporaries, I introduced a regression where types with
an explicit copy constructor could not be passed to std::thread or
std::async. The fix is to add a constructor instead of using aggregate
initialization of an unnamed temporary.

libstdc++-v3/ChangeLog:

PR libstdc++/106695
* include/bits/std_thread.h (thread::_State_impl): Forward
individual arguments to _Invoker constructor.
(thread::_Invoker): Add constructor. Delete copies.
* include/std/future (__future_base::_Deferred_state): Forward
individual arguments to _Invoker constructor.
(__future_base::_Async_state_impl): Likewise.
* testsuite/30_threads/async/106695.cc: New test.
* testsuite/30_threads/thread/106695.cc: New test.

(cherry picked from commit 5abe0657553580bd1b7488dd84d55138a8d9f23c)

libstdc++-v3/include/bits/std_thread.h
libstdc++-v3/include/std/future
libstdc++-v3/testsuite/30_threads/async/106695.cc [new file with mode: 0644]
libstdc++-v3/testsuite/30_threads/thread/106695.cc [new file with mode: 0644]

index 2a500bf177728fadc7d39eb30253b4f5d0e12589..976526001c5afdf0691fa38891d086fde798e67f 100644 (file)
@@ -204,7 +204,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
        template<typename... _Args>
          _State_impl(_Args&&... __args)
-         : _M_func{{std::forward<_Args>(__args)...}}
+         : _M_func(std::forward<_Args>(__args)...)
          { }
 
        void
@@ -238,6 +238,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     template<typename _Tuple>
       struct _Invoker
       {
+       template<typename... _Args>
+         explicit
+         _Invoker(_Args&&... __args)
+         : _M_t(std::forward<_Args>(__args)...)
+         { }
+
        _Tuple _M_t;
 
        template<typename>
index 88478860c7d89650ccbc01a37239deb37344f7ea..7aa243eb625ed7f202066f1ecbed788cafd83b0c 100644 (file)
@@ -1658,7 +1658,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        explicit
        _Deferred_state(_Args&&... __args)
        : _M_result(new _Result<_Res>()),
-         _M_fn{{std::forward<_Args>(__args)...}}
+         _M_fn(std::forward<_Args>(__args)...)
        { }
 
     private:
@@ -1725,7 +1725,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        explicit
        _Async_state_impl(_Args&&... __args)
        : _M_result(new _Result<_Res>()),
-         _M_fn{{std::forward<_Args>(__args)...}}
+         _M_fn(std::forward<_Args>(__args)...)
        {
          _M_thread = std::thread{&_Async_state_impl::_M_run, this};
        }
diff --git a/libstdc++-v3/testsuite/30_threads/async/106695.cc b/libstdc++-v3/testsuite/30_threads/async/106695.cc
new file mode 100644 (file)
index 0000000..7499634
--- /dev/null
@@ -0,0 +1,29 @@
+// { dg-do compile { target c++11 } }
+// { dg-require-gthreads "" }
+
+// PR libstdc++/106695
+// Explicit copy constructor does not work for a parameter passed via std::async
+
+#include <future>
+
+struct A {
+  A() = default;
+  explicit A(const A&) = default;
+};
+
+void func(const A&) { }
+
+void
+test_async()
+{
+  (void) std::async(std::launch::async, func, A{});
+  (void) std::async(std::launch::deferred, func, A{});
+  (void) std::async(func, A{});
+}
+
+void
+test_task()
+{
+  std::packaged_task<void(const A&)> task(func);
+  task(A{});
+}
diff --git a/libstdc++-v3/testsuite/30_threads/thread/106695.cc b/libstdc++-v3/testsuite/30_threads/thread/106695.cc
new file mode 100644 (file)
index 0000000..97e9e92
--- /dev/null
@@ -0,0 +1,21 @@
+// { dg-do compile { target c++11 } }
+// { dg-require-gthreads "" }
+
+// PR libstdc++/106695
+// Explicit copy constructor does not work for a parameter passed via std::async
+
+#include <thread>
+
+struct A {
+  A() = default;
+  explicit A(const A&) = default;
+};
+
+void func(const A&) { }
+
+void
+test_thread()
+{
+  std::thread t(func, A{});
+  t.join();
+}