【发布时间】:2013-12-24 03:15:28
【问题描述】:
1。 我几天前发布了这个问题(About thread-safety of weak_ptr),现在我有另一个相关的问题。 如果我做这样的事情,会在上面的例子中引入一个竞争条件作为 g_w 吗?(我的平台是 ms vs2013)
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.据我所知,从 std::tr1::shared_ptr/weak_ptr 派生的 std::shared_ptr/weak_ptr 和从 boost::shared_ptr/weak_ptr 派生的 std::tr1::shared_ptr/weak_ptr 有什么区别吗?该工具,尤其是在线程安全的救济中。
【问题讨论】:
-
p_s的销毁不会修改g_w。它不会写信给g_w。
标签: multithreading c++11 race-condition weak-ptr