std::shared_ptr<int> int_ptr;
int main() {
int_ptr = std::make_shared<int>(1);
std::thread th{[&]() {
std::weak_ptr int_ptr_weak = int_ptr;
auto int_ptr_local = int_ptr_weak.lock();
if (int_ptr_local) {
cout << "Value in the shared_ptr is " << *int_ptr_local << endl;
}
});
int_ptr.reset(nullptr);
th.join();
return 0;
}
Is the code above thread safe? I read this answer About thread-safety of weak_ptr but just wanted to make sure that the above code is thread safe.
The reason I ask this is that if the code above is indeed thread safe, I cannot understand how the std::weak_ptr
and std::shared_ptr
interfaces make the following operation atomic expired() ? shared_ptr<T>() : shared_ptr<T>(*this)
. It just seems to me that making two logical lines of code like above cannot be made synchronous without using some sort of mutex or spinlock.
I understand how atomic increments work with different instances of shared pointers and I understand that shared_ptr
s themselves are not thread safe, but if the above is indeed thread safe it is very much like a thread safe shared_ptr
and I don't understand how two lines of code like in the conditional above can be made atomic without locks.