爲什麼我的代碼中沒有任何競爭條件? 由於源這裏:http://en.cppreference.com/w/cpp/memory/shared_ptrshared_ptr中的競爭條件不會發生
如果執行訪問多個線程相同的shared_ptr不同步和任何這些訪問的使用的shared_ptr的非const成員函數然後將出現數據爭用;
class base
{
public:
std::string val1;
};
class der : public base
{
public:
std::string val2;
int val3;
char val4;
};
int main()
{
std::mutex mm;
std::shared_ptr<der> ms(new der());
std::thread t1 = std::thread([ms, &mm]() {
while (1)
{
//std::lock_guard<std::mutex> lock(mm);
std::string some1 = ms->val2;
int some2 = ms->val3;
char some3 = ms->val4;
ms->val2 = "1232324";
ms->val3 = 1232324;
ms->val4 = '1';
}
});
std::thread t2 = std::thread([ms, &mm]() {
while (1)
{
//std::lock_guard<std::mutex> lock(mm);
std::string some1 = ms->val2;
int some2 = ms->val3;
char some3 = ms->val4;
ms->val2 = "123435";
ms->val3 = 123435;
ms->val4 = '3';
}
});
std::shared_ptr<base> bms = ms;
std::thread t3 = std::thread([bms]() {
while (1)
{
bms->val1 = 434;
}
});
while (1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
代碼中存在數據競爭。編譯器不會給你一個錯誤,但它只是產生未定義的代碼。 – NathanOliver
你有數據競賽**和**比賽條件,它們是不同的。然而,在你的例子中'shared_ptr'沒有,它全部在'der' –
@Passer By上,但是那裏'std :: shared_ptr'的'operator->'可能有競爭條件。據我所知,爲了避免它,我應該使用'std :: atomic_load'並獲取'std :: shated_ptr'的副本以在另一個'std :: thread'中使用。 –