我想到的嘗試線程消毒劑(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程序是OK(和評價,比如說,即使沒有該程序是種族自由)。 但燦抱怨,並給出了一堆警告: http://www.filedropper.com/output 所以我使用的工具不對,或者是不是真的好? 如果重要我使用VS11測試版。
即使沒有任何同步,程序也是正確的...... – 2012-04-23 02:37:29
我猜想是這樣,我想第一次加入就夠了......我只是想確定一下。 Ofc第二次加入是必要的。 – NoSenseEtAl 2012-04-23 02:47:32
這兩個連接都是需要的,但是您寫入的方式沒有併發性。 – 2012-04-23 03:05:04