public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "danregister at poczta dot fm" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug sanitizer/101978] thread sanitizer false positive when condition variable
Date: Sun, 20 Feb 2022 06:04:37 +0000	[thread overview]
Message-ID: <bug-101978-4-DbiDBKUlwd@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-101978-4@http.gcc.gnu.org/bugzilla/>

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

Daniel Adamski <danregister at poczta dot fm> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |danregister at poczta dot fm

--- Comment #3 from Daniel Adamski <danregister at poczta dot fm> ---
(g++ (Debian 11.2.0-14) 11.2.0)

I believe it is wait_for() in particular. This works fine:

#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
using namespace std::chrono_literals;

std::mutex mtx;
std::condition_variable cv;

void run1() {
    std::unique_lock lck{mtx};
    cv.wait(lck);
}
void run2() {
    std::unique_lock lck{mtx};
    std::this_thread::sleep_for(500ms);
    cv.notify_all();
}
int main() {
    std::jthread th1{ run1 };
    std::jthread th2{ run2 };
}

But replace:

-    cv.wait(lck);
+    cv.wait_for(lck, 1s);

and you get "double lock":

==================
WARNING: ThreadSanitizer: double lock of a mutex (pid=644005)
    #0 pthread_mutex_lock
../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:4240
(libtsan.so.0+0x4f30a)
    #1 __gthread_mutex_lock
/usr/include/x86_64-linux-gnu/c++/11/bits/gthr-default.h:749 (a.out+0x27bf)
    #2 std::mutex::lock() /usr/include/c++/11/bits/std_mutex.h:100
(a.out+0x2844)
    #3 std::unique_lock<std::mutex>::lock()
/usr/include/c++/11/bits/unique_lock.h:139 (a.out+0x422d)
    #4 std::unique_lock<std::mutex>::unique_lock(std::mutex&)
/usr/include/c++/11/bits/unique_lock.h:69 (a.out+0x3ce2)
    #5 run2() /tmp/tsanmy.cpp:16 (a.out+0x241d)
    #6 void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void
(*&&)()) /usr/include/c++/11/bits/invoke.h:61 (a.out+0x5550)
    #7 std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void
(*&&)()) /usr/include/c++/11/bits/invoke.h:96 (a.out+0x54b5)
    #8 void std::thread::_Invoker<std::tuple<void (*)()>
>::_M_invoke<0ul>(std::_Index_tuple<0ul>)
/usr/include/c++/11/bits/std_thread.h:253 (a.out+0x541a)
    #9 std::thread::_Invoker<std::tuple<void (*)()> >::operator()()
/usr/include/c++/11/bits/std_thread.h:260 (a.out+0x53c4)
    #10 std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> >
>::_M_run() /usr/include/c++/11/bits/std_thread.h:211 (a.out+0x537e)
    #11 <null> <null> (libstdc++.so.6+0xd38f3)

  Location is global 'mtx' of size 40 at 0x56501849e160 (a.out+0x000000009160)

  Mutex M10 (0x56501849e160) created at:
    #0 pthread_mutex_lock
../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:4240
(libtsan.so.0+0x4f30a)
    #1 __gthread_mutex_lock
/usr/include/x86_64-linux-gnu/c++/11/bits/gthr-default.h:749 (a.out+0x27bf)
    #2 std::mutex::lock() /usr/include/c++/11/bits/std_mutex.h:100
(a.out+0x2844)
    #3 std::unique_lock<std::mutex>::lock()
/usr/include/c++/11/bits/unique_lock.h:139 (a.out+0x422d)
    #4 std::unique_lock<std::mutex>::unique_lock(std::mutex&)
/usr/include/c++/11/bits/unique_lock.h:69 (a.out+0x3ce2)
    #5 run1() /tmp/tsanmy.cpp:11 (a.out+0x2392)
    #6 void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void
(*&&)()) /usr/include/c++/11/bits/invoke.h:61 (a.out+0x5550)
    #7 std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void
(*&&)()) /usr/include/c++/11/bits/invoke.h:96 (a.out+0x54b5)
    #8 void std::thread::_Invoker<std::tuple<void (*)()>
>::_M_invoke<0ul>(std::_Index_tuple<0ul>)
/usr/include/c++/11/bits/std_thread.h:253 (a.out+0x541a)
    #9 std::thread::_Invoker<std::tuple<void (*)()> >::operator()()
/usr/include/c++/11/bits/std_thread.h:260 (a.out+0x53c4)
    #10 std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> >
>::_M_run() /usr/include/c++/11/bits/std_thread.h:211 (a.out+0x537e)
    #11 <null> <null> (libstdc++.so.6+0xd38f3)

SUMMARY: ThreadSanitizer: double lock of a mutex
/usr/include/x86_64-linux-gnu/c++/11/bits/gthr-default.h:749 in
__gthread_mutex_lock

  parent reply	other threads:[~2022-02-20  6:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-19 14:31 [Bug sanitizer/101978] New: thread sanitizer false positive when smart pointers ispavlick at gmail dot com
2021-08-19 16:59 ` [Bug sanitizer/101978] " ispavlick at gmail dot com
2021-08-20  7:53 ` [Bug sanitizer/101978] thread sanitizer false positive when condition variable marxin at gcc dot gnu.org
2022-02-20  6:04 ` danregister at poczta dot fm [this message]
2022-07-18 11:35 ` boris at kolpackov dot net
2022-07-28 15:36 ` lewis at sophists dot com
2022-09-23 14:52 ` jakob.weisblat at zoom dot us
2022-09-23 16:44 ` pinskia at gcc dot gnu.org
2022-09-23 16:47 ` jakob.weisblat at zoom dot us
2022-09-23 17:19 ` redi at gcc dot gnu.org
2022-09-23 18:30 ` redi at gcc dot gnu.org

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=bug-101978-4-DbiDBKUlwd@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).