From: Jonathan Wakely <jwakely@redhat.com>
To: "libstdc++" <libstdc++@gcc.gnu.org>
Subject: std::jthread move assignment
Date: Thu, 23 Mar 2023 13:52:35 +0000 [thread overview]
Message-ID: <CACb0b4=FredKPd=QOHpdO-yUFK1s+u=k2LfVY-UrGspciqAS2w@mail.gmail.com> (raw)
While checking if we need to do anything for
https://cplusplus.github.io/LWG/issue3788 I noticed that our
std::jthread move assignment doesn't match the spec in the standard.
The effects (after LWG 3788) are:
If &x == this is true, there are no effects. Otherwise, if joinable()
is true, calls request_stop() and then join(), then assigns the state
of x to *this and sets x to a default constructed state.
Our implementation is:
jthread&
operator=(jthread&& __other) noexcept
{
std::jthread(std::move(__other)).swap(*this);
return *this;
}
This performs the effects in reverse! First we set other to a
default-constructed state (moving it to a temporary), then we assign
its original state to *this (by swapping with the temporary), then if
the original state of *this was joinable() we request_stop() and
join() (in the temporary's destructor).
In theory the other thread could inspect the state of either *this or
other when requested to stop (e.g. because it has a reference to the
jthread, or the jthread is a global). If the jthread has been modified
before the request_stop() call, that's observable. However, I think
that would be a data race because we're in the process of calling
non-const member functions on both *this and other. So if the running
thread accesses either of those objects, the behaviour is undefined.
So it's OK that we do the effects in a different order.
Does anybody disagree?
reply other threads:[~2023-03-23 13:52 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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='CACb0b4=FredKPd=QOHpdO-yUFK1s+u=k2LfVY-UrGspciqAS2w@mail.gmail.com' \
--to=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).