From: Paul Scharnofske <asynts@gmail.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: libstdc++@gcc.gnu.org
Subject: Re: std::jthread::operator=(std::jthread&&) calls std::terminate if *this has an associated running thread.
Date: Sun, 08 Nov 2020 14:51:00 +0100 [thread overview]
Message-ID: <5fa7f7cf.1c69fb81.d14a.c5c9@mx.google.com> (raw)
In-Reply-To: <20201106220252.GM503596@redhat.com>
>I think this would work:
>
> jthread& operator=(jthread&& __x) noexcept
> {
> std::jthread(std::move(__x)).swap(*this);
> return *this;
> }
That looks a lot better than what I did, it's also consistent with other
places like std::stop_token::operator=(std::stop_token&&).
I updated my patch and also created a test that checks the post
conditions described in the standard.
---
libstdc++-v3/include/std/thread | 6 +++++-
.../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();
}
--
2.29.2
next prev parent reply other threads:[~2020-11-08 13:51 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-06 21:13 Paul Scharnofske
2020-11-06 21:35 ` Ville Voutilainen
2020-11-06 21:49 ` Paul Scharnofske
2020-11-06 22:02 ` Jonathan Wakely
2020-11-08 13:51 ` Paul Scharnofske [this message]
2020-11-11 11:15 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5fa7f7cf.1c69fb81.d14a.c5c9@mx.google.com \
--to=asynts@gmail.com \
--cc=jwakely@redhat.com \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).