我做了一些谷歌,似乎无法为此发现GCC选项或libstdc ++宏.是否可以在所有std::atomic模板特化上强制使用内部锁定.在某些平台上,一些专业化无论如何都是锁定的,因此它看起来似乎是一个可行的选择.
在过去,我发现在使用Valgrind(Helgrind或DRD)std::atomic等工具调试数据竞争时,由于大量的误报,使用起来非常痛苦.如果使用atomics足够普遍,抑制文件似乎不是一个非常可扩展的解决方案.
我想过试用线程清理程序( 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();
}
Run Code Online (Sandbox Code Playgroud)
AFAIK程序没问题,因为违反的访问权限与mutex同步(并且评论说即使没有该程序也没有竞争).但tsan抱怨并提出了一些警告:http: //www.filedropper.com/output 所以我使用的工具是错误的,还是不是很好?如果重要,我正在使用VS11 Beta.