public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/103382] New: condition_variable::wait() is not cancellable because it is marked noexcept
@ 2021-11-23 13:01 vanyacpp at gmail dot com
  2021-11-23 13:06 ` [Bug libstdc++/103382] " vanyacpp at gmail dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: vanyacpp at gmail dot com @ 2021-11-23 13:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103382

            Bug ID: 103382
           Summary: condition_variable::wait() is not cancellable because
                    it is marked noexcept
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vanyacpp at gmail dot com
  Target Milestone: ---

At the moment condition_variable::wait() is marked noexcept. It means that if
pthread_cond_wait() acts as cancellation point and throws an exception the
program is terminated with std::terminate(). This program demonstrates the
issue:

#include <condition_variable>
#include <thread>

int main()
{
    std::mutex m;
    std::condition_variable cv;

    std::thread th([&]
    {
        std::unique_lock lock(m);
        cv.wait(lock);
    });

    pthread_cancel(th.native_handle());
    th.join();
}

This program terminates with SIGABRT.

Because of this using condition_variable::wait() in cancellable threads is
tricky: the programmer has to guard all calls to condition_variable::wait()
with disabling/restoring cancellation state. Also this stops the thread from
being cancellable while in wait(). Therefore the outer thread has to know which
condition_variable the thread waits and notify this condition_variable after
pthread_cancel(). Also one should add cancellation point pthread_testcancel()
immediately after restoring cancellation state after wait().

I believe it would be great if condition_variable::wait interacted nicer with
POSIX-cancellation. I would like to suggest removing noexcept from
condition_variable::wait(). This also matches the C++ standard very well
[thread.condition.condvar] where condition_variable::wait() is not marked as
noexcept.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-09-06 12:53 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-23 13:01 [Bug libstdc++/103382] New: condition_variable::wait() is not cancellable because it is marked noexcept vanyacpp at gmail dot com
2021-11-23 13:06 ` [Bug libstdc++/103382] " vanyacpp at gmail dot com
2021-11-23 13:44 ` redi at gcc dot gnu.org
2021-11-24 14:46 ` vanyacpp at gmail dot com
2021-11-24 22:10 ` redi at gcc dot gnu.org
2021-12-09 23:27 ` cvs-commit at gcc dot gnu.org
2021-12-09 23:38 ` redi at gcc dot gnu.org
2022-09-06 12:35 ` vanyacpp at gmail dot com
2022-09-06 12:53 ` redi at gcc dot gnu.org

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