c++ - Thread Sanitizer 是否可用?

标签 c++ race-condition

我想尝试线程清洁剂 ( http://code.google.com/p/data-race-test/wiki/ThreadSanitizer#Using_ThreadSanitizer ) 所以我做了一个简单的程序:

#include <thread>
#include <atomic>
#include <vector>
#include <iostream>
#include <algorithm>
#include <mutex>
using namespace std;
int violated=0;
mutex mtx;
void violator()
{
    lock_guard<mutex> lg(mtx);
    violated++;
}
int main()
{
    thread t1(violator);
    t1.join();
    thread t2(violator);
    t2.join();
}

AFAIK 程序是可以的,因为对 violated 的访问与互斥锁同步(就像评论说的那样,即使没有该程序也是免费的)。 但是 tsan 提示并给出了一堆警告: http://www.filedropper.com/output 那么我是用错了这个工具,还是它不是很好? 如果重要的话,我正在使用 VS11 Beta。

最佳答案

这很正常,ThreadSanitizer 不知道如何正确处理 C++11 线程库,它也无法使用 Interlocked* 或 std::atomic 处理细粒度同步。此外,混合模式会产生误报。您可以构建抑制文件以忽略标准库中的竞争和其他误报。在 linux x64 和 ThreadSanitizer 上使用你的代码,我在 stdlib 中得到了 7 个错误的比赛。添加一个suppression file之后我能够忽略这些种族。然后我删除了你的锁并将你的 t1.join() 移动到第二个线程开始之后(所以有一场真正的比赛。)ThreadSanitizer 正确地检测到这一点。然后我重新添加了你的互斥量,并且不再报告比赛。所以它实际上看起来非常有用。 Google 使用它在他们的 Chrome 浏览器和许多其他项目中查找比赛,因此它非常成熟(尽管在我的 ubuntu 12.10 系统上构建它真的很痛苦。)

对于 linux,我的抑制文件如下所示:

{
<std::shared_ptr>
ThreadSanitizer:Race
...
fun:std::_Sp_counted_base::_M_release
fun:std::__shared_count::~__shared_count
fun:std::__shared_ptr::~__shared_ptr
}
{
<std::arena_thread_freeres>
ThreadSanitizer:Race
fun:arena_thread_freeres
fun:__libc_thread_freeres
fun:start_thread
}

关于c++ - Thread Sanitizer 是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10274036/

相关文章:

c++ - 用 QT 线程解决运行时问题

python - 为什么 Python threading.Condition() notify() 需要锁?

c++ - Process Explorer 中的可拖动十字线如何工作?

c++ - 在 debian linux 中使用 c 复制文件

javascript - 在多个 chrome.storage API 调用中防止竞争条件的最佳方法?

javascript - Firefox 引导扩展,导入顶级时的竞争条件?

java - 数据库序列如何管理竞争条件?

c++ - boost 序列化: ensure data safety over socket transmition

c++ - 在已经使用 tensorflow 作为第三方的 c++ 库中使用新的 tensorflow op

c++ - #pragma warning 不抑制警告