public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Paul Scharnofske <asynts@gmail.com>
To: Ville Voutilainen <ville.voutilainen@gmail.com>
Cc: libstdc++ <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:49:09 +0100	[thread overview]
Message-ID: <c4bdd25d-4689-226a-15b6-5de1dd144f0c@gmail.com> (raw)
In-Reply-To: <CAFk2RUaQXGkWFc6KU3SRuROvntgYK9R+ABa_CYLVvFnSMYnrtg@mail.gmail.com>

You are correct, I was not aware of this 
(https://stackoverflow.com/a/36393265/8746648).

Here is the simplified patch (mirror: 
https://static.asynts.com/2020/11/06/jthread-0002.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.
+
  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..a5d60fe7f9f 100644
--- a/libstdc++-v3/include/std/thread
+++ b/libstdc++-v3/include/std/thread
@@ -456,7 +456,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
      operator=(const jthread&) = delete;

      jthread&
-    operator=(jthread&&) noexcept = default;
+    operator=(jthread&& __other) noexcept
+    {
+      if (joinable())
+        {
+          request_stop();
+          join();
+        }
+
+      swap(__other);
+
+      return *this;
+    }

      void
      swap(jthread& __other) noexcept

On 11/6/20 10:35 PM, Ville Voutilainen wrote:
> On Fri, 6 Nov 2020 at 23:14, Paul Scharnofske via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
>>       +          // 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();
>>       +            }
>>       +        }
> Just calling join() will have the same effect, without having to
> bother with the additional catching.


  reply	other threads:[~2020-11-06 21:49 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 [this message]
2020-11-06 22:02 ` Jonathan Wakely
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=c4bdd25d-4689-226a-15b6-5de1dd144f0c@gmail.com \
    --to=asynts@gmail.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=ville.voutilainen@gmail.com \
    /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).