From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 0D72639878E0; Wed, 11 Nov 2020 11:13:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0D72639878E0 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-4910] libstdc++: Assigning to a joinable std::jthread calls std::terminate X-Act-Checkin: gcc X-Git-Author: Paul Scharnofske X-Git-Refname: refs/heads/master X-Git-Oldrev: 43f9e5aff06f1ca2296fdbd3141fe90ec0be1912 X-Git-Newrev: 0ebaea3b6677ef8edfa5638800304db1a4f7c2f8 Message-Id: <20201111111348.0D72639878E0@sourceware.org> Date: Wed, 11 Nov 2020 11:13:48 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Nov 2020 11:13:48 -0000 https://gcc.gnu.org/g:0ebaea3b6677ef8edfa5638800304db1a4f7c2f8 commit r11-4910-g0ebaea3b6677ef8edfa5638800304db1a4f7c2f8 Author: Paul Scharnofske Date: Wed Nov 11 09:29:37 2020 +0000 libstdc++: Assigning to a joinable std::jthread calls std::terminate Move assigning to a std::jthread that represents a thread of execution needs to send a stop request and join that running thread. Otherwise the std::thread data member will terminate in its assignment operator. Co-authored-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/std/thread (jthread::operator=(jthread&&)): Transfer any existing state to a temporary that will request a stop and then join. * testsuite/30_threads/jthread/jthread.cc: Test move assignment. Diff: --- libstdc++-v3/include/std/thread | 6 +++++- libstdc++-v3/testsuite/30_threads/jthread/jthread.cc | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/thread b/libstdc++-v3/include/std/thread index 887ee579962..080036e2609 100644 --- a/libstdc++-v3/include/std/thread +++ b/libstdc++-v3/include/std/thread @@ -456,7 +456,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator=(const jthread&) = delete; jthread& - operator=(jthread&&) noexcept = default; + operator=(jthread&& __other) noexcept + { + std::jthread(std::move(__other)).swap(*this); + return *this; + } void swap(jthread& __other) noexcept diff --git a/libstdc++-v3/testsuite/30_threads/jthread/jthread.cc b/libstdc++-v3/testsuite/30_threads/jthread/jthread.cc index 746ff437c1d..b8ba62f6df2 100644 --- a/libstdc++-v3/testsuite/30_threads/jthread/jthread.cc +++ b/libstdc++-v3/testsuite/30_threads/jthread/jthread.cc @@ -187,6 +187,25 @@ void test_detach() VERIFY(t1FinallyInterrupted.load()); } +//------------------------------------------------------ + +void test_move_assignment() +{ + std::jthread thread1([]{}); + std::jthread thread2([]{}); + + const auto id2 = thread2.get_id(); + const auto ssource2 = thread2.get_stop_source(); + + thread1 = std::move(thread2); + + VERIFY(thread1.get_id() == id2); + VERIFY(thread2.get_id() == std::jthread::id()); + + VERIFY(thread1.get_stop_source() == ssource2); + VERIFY(!thread2.get_stop_source().stop_possible()); +} + int main() { std::set_terminate([](){ @@ -197,4 +216,5 @@ int main() test_stop_token(); test_join(); test_detach(); + test_move_assignment(); }