1. 幾天前我發佈了問題(About thread-safety of weak_ptr),現在我有其他相關問題。 如果我這樣做,將引入競爭條件,如上面的例子g_w?(我的平臺是毫秒vs2013)關於weak_ptr的競賽條件
std::weak_ptr<int> g_w;
void f3()
{
std::shared_ptr<int>l_s3 = g_w.lock(); //2. here will read g_w
if (l_s3)
{
;/.....
}
}
void f4() //f4 run in main thread
{
std::shared_ptr<int> p_s = std::make_shared<int>(1);
g_w = p_s;
std::thread th(f3); // f3 run in the other thread
th.detach();
// 1. p_s destory will motify g_w (write g_w)
}
2.As我知道的std :: shared_ptr的/ weak_ptr的衍生的std :: TR1 :: shared_ptr/weak_ptr和從boost :: shared_ptr/weak_ptr派生的std :: tr1 :: shared_ptr/weak_ptr,實現上是否有區別,特別是在線程安全的情況下。
p_s'的'破壞將不會修改'g_w'。它不會寫入'g_w'。 –