public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Paul Scharnofske <asynts@gmail.com>
Cc: libstdc++@gcc.gnu.org
Subject: Re: std::jthread::operator=(std::jthread&&) calls std::terminate if *this has an associated running thread.
Date: Fri, 6 Nov 2020 22:02:52 +0000	[thread overview]
Message-ID: <20201106220252.GM503596@redhat.com> (raw)
In-Reply-To: <7388faf6-f5e9-1328-cbe5-e8634e092561@gmail.com>

On 06/11/20 22:13 +0100, Paul Scharnofske via Libstdc++ wrote:
>Disclaimer 1: That C++ Standard stuff seems really complicated, I only 
>really looked at
>https://cppreference.com and then verified it with 
>https://github.com/cplusplus/draft . I hope
>I got this right.
>
>Disclaimer 2: I've never send anything to a mailing list, I hope this works.
>
>The following snippet does the wrong thing:
>
>    #include <thread>
>
>    int main() {
>        std::jthread thread { [] {} };
>
>        // Calls std::terminate in std::thread::operator=(std::thread&&).
>        // Should instead join the previous thread before doing the 
>assignment.
>        thread = std::jthread{ [] {} };
>    }
>
>This looks like an oversight to me, from the standard:
>
>    32.4.4.2 Constructors, move, and assignment [thread.jthread.cons]
>
>    jthread& operator=(jthread&& x) noexcept;
>
>        Effects: If joinable() is true, calls request_stop() and then 
>join().
>        Assigns the state of x to *this and sets x to a default constructed
>        state. [...]
>
>    [...]
>
>    32.4.3.5 Assignment [thread.thread.assign]
>
>    thread& operator=(thread&& x) noexcept;
>
>        Effects: If joinable(), invokes terminate (14.6.2). Otherwise, 
>assigns
>        the state of x to *this and sets x to a default constructed state.
>        [...]
>
>I suggest the following fix: (mirror: 
>https://static.asynts.com/2020/11/06/jthread.patch)
>
>    diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
>    index 89f9f6c8c38..02e4c3cc8a6 100644
>    --- a/libstdc++-v3/ChangeLog
>    +++ b/libstdc++-v3/ChangeLog
>    @@ -1,3 +1,8 @@
>    +2020-11-06  Paul Scharnofske  <asynts@gmail.com>
>    +
>    +       * include/std/thread (operator=(std::jthread&&): Join 
>current thread if it
>    +       is running before moving it.
>    +

The ChangeLog file is autogenerated from the Git commit logs, please
don't change it in patches (just provide the suggested commit log
message).

>     2020-11-05  Marek Polacek  <polacek@redhat.com>
>
>            PR c++/25814
>    diff --git a/libstdc++-v3/include/std/thread 
>b/libstdc++-v3/include/std/thread
>    index 887ee579962..773befa75ad 100644
>    --- a/libstdc++-v3/include/std/thread
>    +++ b/libstdc++-v3/include/std/thread
>    @@ -456,7 +456,29 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>         operator=(const jthread&) = delete;
>
>         jthread&
>    -    operator=(jthread&&) noexcept = default;
>    +    operator=(jthread&& __other) noexcept
>    +    {
>    +      if (joinable())
>    +        {
>    +          request_stop();
>    +
>    +          // The C++ Standard (working draft) says that this 
>method must be
>    +          // noexcept, but also dictates that join be called. It 
>doesn't say
>    +          // how to do this, this is probably the way to go?
>    +          try
>    +            {
>    +              join();
>    +            }
>    +          catch (...)
>    +            {
>    +              std::terminate();
>    +            }
>    +        }
>    +
>    +      swap(__other);

Using swap doesn't seem to meet the requirement that __other is set to
a default constructed state, because __other._M_stop_state will still
have a shared state.

I think this would work:

   jthread& operator=(jthread&& __x) noexcept
   {
     std::jthread(std::move(__x)).swap(*this);
     return *this;
   }

It would modify __x before calling request_stop() and join(), which is
potentially observable from the thread being joined. But since this is
modifying __x, it's a data race for the other thread to access it
concurrently, which would have undefined behaviour. So I think it's
OK.


>    +
>    +      return *this;
>    +    }
>
>         void
>         swap(jthread& __other) noexcept
>
>Like already mentioned in the comment, there is a bit of an inconsistency in
>the Standard since the move assignment operator is marked as noexcept 
>and the join
>function can throw an exception...
>
>I don't know if there is something in the standard that dictates what 
>should happen in such
>a case, but I really don't know where to look for it.
>


  parent reply	other threads:[~2020-11-06 22:02 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 [this message]
2020-11-08 13:51 ` Paul Scharnofske
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=20201106220252.GM503596@redhat.com \
    --to=jwakely@redhat.com \
    --cc=asynts@gmail.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).