Hello,
Is it possible to use ThreadSanitizer with std::mutex and related classes (std::unique_lock, std::lock_guard, etc.)?
The following example:
// minimal.cpp
#include <thread>
#include <mutex>
std::mutex g_mutex_;
int g_var = 0;
void update() {
std::lock_guard<std::mutex>{g_mutex_};
g_var = 1;
}
int use() {
std::lock_guard<std::mutex>{g_mutex_};
return g_var + 1;
}
int main() {
std::thread t{update};
use();
t.join();
return 0;
}
Compiled with:
clang++-9 -o system-std1 -g -pie -fPIE -fsanitize=thread minimal.cpp
Gives a data race warning. However, if I replace the std locks with phtead locks, the warning disappears.
I tried compiling and linking libc++ with “-fsanitize=thread” but the warning persists.
Libc++ was compiled from the tag ‘llvmorg-9.0.0’ of https://github.com/llvm/llvm-project
mkdir build && cd build
cmake -DCMAKE_C_COMPILER=clang-9 \
-DCMAKE_CXX_COMPILER=clang++-9 \
-DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" \
-DLLVM_USE_SANITIZER=Thread \
../llvm
make -j4
minimal.cpp was compiled with
clang++-9 -o std-with-tsan -stdlib=libc++ -nostdinc++ -g -pie -fPIE -fsanitize=thread \
-I/home/vsarchelu/src/llvm-project-llvmorg-9.0.0/build-tsan/include/c++/v1 -L/home/vsarchelu/src/llvm-project-llvmorg-9.0.0/build-tsan/lib -Wl,-rpath,/home/vsarchelu/src/llvm-project-llvmorg-9.0.0/build-tsan/lib minimal.cpp
I am happy to provide more info if anyone wants to take a look at this.
Kind regards,
Herman Lundkvist
P.S. Some system info:
Linux wm-ware guest os on windows host.
➜ clang++-9 --version
clang version 9.0.0-2~ubuntu18.04.1 (tags/RELEASE_900/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
➜ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.3 LTS
Release: 18.04
Codename: bionic