Open kyle-figure opened 1 year ago
Isn't it because the destructor of lock variable also calls m.unlock() at the end of the scope which means the lock_ is unlocked twice?
Isn't it because the destructor of lock variable also calls m.unlock() at the end of the scope which means the lock_ is unlocked twice?
The unique lock object does not hold the mutex after instantiation, due to std::defer_lock. If what you are saying was the issue here, try_lock should also fail in the same way. However, try_lock works and try_lock_for does not.
However, try_lock works and try_lock_for does not.
Okay, that's weird. BTW in my understanding, unique_lock with defer_lock just skips initial lock() at instantiation and ensures unlock() at destruction in RAII manner.
Is there update on this? I just hit the same issue.
Same here with -fsanitize=thread will throw a tsan error at the first iteration:
#include <chrono>
#include <mutex>
#include <thread>
using namespace std::chrono_literals;
int main(int argc, char **argv) {
(void)argc; (void)argv;
std::timed_mutex m;
while (true) {
if (m.try_lock_for(100ms))
{
m.unlock();
}
std::this_thread::sleep_for(5ms);
}
return 0;
}
while the same with valgrind --tool=helgrind
works fine. I use llvm version 20.
Description: A false positive occurs when using either
try_lock_for
ortry_lock_until
with astd::timed_mutex
object. In the code snippet provided, the mutex can only be unlocked by the thread which locks it, yet ThreadSanitizer reports a warning. I have tried usingstd::timed_mutex
with the simpletry_lock
function, and it works without ThreadSanitizer reporting a warning.Platform: Ubuntu 22.04 AMD64
main.cpp
Compile command:
Execution output:
clang++ version: