Is Thread Sanitizer usable?

巧了我就是萌 提交于 2019-12-21 05:38:15

问题


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 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.


回答1:


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.)

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
}


来源:https://stackoverflow.com/questions/10274036/is-thread-sanitizer-usable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!