线程消毒剂是否可用? [英] Is Thread Sanitizer usable?

查看:74
本文介绍了线程消毒剂是否可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I thought of trying out thread sanitizer ( http://code.google.com/p/data-race-test/wiki/ThreadSanitizer#Using_ThreadSanitizer ) so I made a simple program:

#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 程序是可以的,因为对违反的访问与互斥锁同步(就像评论说即使没有该程序也是免费的).但是 tsan 抱怨并给出了一堆警告:http://www.filedropper.com/output那么我使用该工具是错误的,还是不是很好?如果重要的话,我正在使用 VS11 Beta.

AFAIK program is OK since access to violated is synced with mutex(and like comments say even without that program is race free). But tsan complains and gives a bunch of warnings: http://www.filedropper.com/output So am I using the tool wrong, or is it not really good ? If important I'm using VS11 Beta.

推荐答案

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

This is normal, ThreadSanitizer does not know how to deal properly with the C++11 threading libraries, it also can't handle fine-grained synchronization using Interlocked* or std::atomic. Additionally the hybrid mode can produce false positives. You can build a suppression file to ignore races in the standard libraries and other false positives. Using your code on linux x64 and ThreadSanitizer I got 7 false races in the stdlib. After adding a suppression file I was able to ignore these races. Then I removed your lock and moved your t1.join() to after the start of the second thread (so there is a real race.) ThreadSanitizer detects this correctly. Then I added your mutex back in and the race was no longer reported. So it does in fact seem quite useful. Google uses it to find races in their Chrome browser, among many other projects, so it's quite mature (although building it on my ubuntu 12.10 system was a real pain.)

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

For linux my suppression file looks like:

{
<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
}

这篇关于线程消毒剂是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆