Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason for the name `weak_ptr::lock()`?

Many of our developers don't understand what creating a shared_ptr from a weak_ptr has to do with locking things. By them, the term 'lock' is associated with mutexes first.

It could have been called use or safeguard or lease or promote for instance... but hey, it isn't, and it's our responsibility to learn the standard.

But to them, this is so bad an issue that another class that copied this idiom had to be renamed, causing numerous lines of code to follow the rename. Now we have sacrificed consistency with the standard for just a little intuitivity.

Does anyone know how the choice for the name lock() was made?

like image 294
xtofl Avatar asked Sep 05 '25 03:09

xtofl


1 Answers

It locks the shared object in memory, and prevents it being deleted.

It has nothing to do with locking a mutex or anything like that.

like image 69
Martin Bonner supports Monica Avatar answered Sep 08 '25 04:09

Martin Bonner supports Monica