Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Thread Sanitizer usable?

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.

like image 858
NoSenseEtAl Avatar asked Dec 17 '22 01:12

NoSenseEtAl


1 Answers

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
}
like image 54
Eloff Avatar answered Jan 04 '23 09:01

Eloff