public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* std::jthread move assignment
@ 2023-03-23 13:52 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-03-23 13:52 UTC (permalink / raw)
  To: libstdc++

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?


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-23 13:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-23 13:52 std::jthread move assignment Jonathan Wakely

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).