关于weak_ptr的线程安全 [英] About thread-safety of weak_ptr
问题描述
std :: shared_ptr< int> g_s = std :: make_shared< int>(1);
void f1()
{
std :: shared_ptr< int> l_s1 = g_s; // read g_s
}
void f2()
{
std :: shared_ptr< int> l_s2 = std :: make_shared< int>(3);
std :: thread th(f1);
th.detach();
g_s = l_s2; // write g_s
}
关于上面的代码,我知道不同的线程读写同样 shared_ptr
会导致竞态条件。但是 weak_ptr
怎么样?下面的代码中是否有任何竞争条件? (我的平台是Microsoft VS2013。)
std :: weak_ptr< int& g_w;
void f3()
{
std :: shared_ptr< int> l_s3 = g_w.lock // 2。这里将读取g_w
if(l_s3)
{
; / ...
}
}
void f4
{
std :: shared_ptr< int> p_s = std :: make_shared< int>(1);
g_w = p_s;
std :: thread th(f3);
th.detach();
// 1. p_s destory会激励g_w(写g_w)
}
我知道我迟到了,但是在搜索weak_ptr thread时就出现了,而Casey的回答并不是全部的真相。 shared_ptr
和 weak_ptr
可以在没有进一步同步的情况下使用。
对于 shared_ptr
,有很多文档(例如 cppreference.com 或 stackoverflow )。您可以安全地访问指向来自不同线程的同一对象的 shared_ptr
。你只是不能在两个线程的同一个指针。换句话说:
//从两个线程使用p和p_copy就好了。
//从两个线程使用p或从两个线程使用p和p_ref是非法的。
std :: shared_ptr< A> p = std :: make_shared< A>();
std :: shared_ptr< A> & p_ref = p;
std :: shared_ptr< A> p_copy = p;
要解决代码中的问题,请传递 g_s
作为参数(按值)*到 f1()
。
对于弱指针,安全保证隐藏在 weak_ptr :: lock 的文档中:
有效地返回
expired()? shared_ptr< T>():shared_ptr< T>(* this)
您可以使用 weak_ptr :: lock()
c $ c> shared_ptr 从其他线程无需进一步同步。这也可以在Boost的此处中确认,并在 Chris Jester-Young的这个SO回答。
同样,您必须确保不要在从另一个线程访问它时修改同一个 weak_ptr
,因此将 g_w
传入 f3 ()
。
std::shared_ptr<int> g_s = std::make_shared<int>(1);
void f1()
{
std::shared_ptr<int>l_s1 = g_s; // read g_s
}
void f2()
{
std::shared_ptr<int> l_s2 = std::make_shared<int>(3);
std::thread th(f1);
th.detach();
g_s = l_s2; // write g_s
}
Regarding the code above, I know different threads reading and writing the same shared_ptr
leads to race conditions. But how about weak_ptr
? Is there any race condition in the code below? (My platform is Microsoft 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()
{
std::shared_ptr<int> p_s = std::make_shared<int>(1);
g_w = p_s;
std::thread th(f3);
th.detach();
// 1. p_s destory will motify g_w (write g_w)
}
I know I'm late, but this comes up when searching for "weak_ptr thread", and Casey's answer just isn't the whole truth. Both shared_ptr
and weak_ptr
can be used from threads without further synchronization.
For shared_ptr
, there's a lot of documentation (e.g. on cppreference.com or on stackoverflow). You can safely access shared_ptr
's that point to the same object from different threads. You just can't bang on the same pointer from two threads. In other words:
// Using p and p_copy from two threads is fine.
// Using p from two threads or p and p_ref from two threads is illegal.
std::shared_ptr<A> p = std::make_shared<A>();
std::shared_ptr<A> &p_ref = p;
std::shared_ptr<A> p_copy = p;
To solve that problem in your code, pass g_s
as parameter (by value)* to f1()
.
For weak pointers, the safety guarantee is hidden in the documentation for weak_ptr::lock:
Effectively returns
expired() ? shared_ptr<T>() : shared_ptr<T>(*this)
, executed atomically.
You can use weak_ptr::lock()
to get a shared_ptr
from other threads without further synchronization. This is also confirmed here for Boost and in this SO answer by Chris Jester-Young.
Again, you have to make sure not to modify the same weak_ptr
from one thread while accessing it from another, so pass g_w
into f3()
by value as well.
这篇关于关于weak_ptr的线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!