[libcxx-commits] [libcxx] [libc++] Add thread safety annotations for std::lock (PR #154078)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 3 11:14:07 PDT 2025


PiJoules wrote:

It looks like this patch leads to `-Wthread-safety` incorrectly diagnosing a mutex held by `unique_lock`:

```
#include <mutex>

std::mutex m0, m1;

void func() {
  std::unique_lock ul1(m0, std::defer_lock);
  std::unique_lock ul2(m1, std::defer_lock);
  std::lock(ul1, ul2);
}
```

```
$ ~/misc/clang-cipd-latest/bin/clang++ -c -Wall -Wthread-safety /tmp/test.cc
/tmp/test.cc:9:1: warning: mutex 'ul1' is still held at the end of function [-Wthread-safety-analysis]
    9 | }
      | ^
/tmp/test.cc:8:3: note: mutex acquired here
    8 |   std::lock(ul1, ul2);
      |   ^
/tmp/test.cc:9:1: warning: mutex 'ul2' is still held at the end of function [-Wthread-safety-analysis]
    9 | }
      | ^
/tmp/test.cc:8:3: note: mutex acquired here
    8 |   std::lock(ul1, ul2);
      |   ^
2 warnings generated.
```

Do more `_LIBCPP_NO_THREAD_SAFETY_ANALYSIS`s need to be added elsewhere to ensure these warnings don't pop up?

https://github.com/llvm/llvm-project/pull/154078


More information about the libcxx-commits mailing list