Re:A safe, non-owning C++ pointer class
by TinkersW on Sunday September 28, 2025 @03:19PM
(#45407049)
They never mention std::weak_ptr which makes me think they aren't aware of it.. yes this looks pretty useless and unsafe(isn't everything multi-threaded these days..)
[ Parent
| on HN ]
> isn't everything multi-threaded these days..
There are alternative ways to utilize a machine with multiple cores, e.g. by running one thread per CPU core, and not sharing state between those threads; in each such thread you then have single-thread "semantics".
Re:A safe, non-owning C++ pointer class
by mwkaufma on Sunday September 28, 2025 @04:13PM
(#45407562)
weak_ptr supports this -- it's only mt-safe if you specialize it with std::atomic
Re:A safe, non-owning C++ pointer class
by vlovich123 on Monday September 29, 2025 @12:52AM
(#45410428)
Last I checked weak_ptr is always atomic (ignoring weird attempted glibc magic when you don’t link against pthread)
Re:A safe, non-owning C++ pointer class
by vlovich123 on Monday September 29, 2025 @04:59PM
(#45418684)
Oh sure, a single weak_ptr instance itself is not safe for multiple concurrent access of non-const methods. But weak_ptr -> shared_ptr reacquisition is atomic and all control block operations are:
> Note that the control block used by std::weak_ptr and std::shared_ptr is thread-safe: different non-atomic std::weak_ptr objects can be accessed using mutable operations, such as operator= or reset, simultaneously by multiple threads, even when these instances are copies or otherwise share the same control block internally. The type T may be an incomplete type.
There’s no variant of shared_ptr / weak_ptr that is non atomic in the standard library AFAIK.
Re:A safe, non-owning C++ pointer class
by spacechild1 on Sunday September 28, 2025 @05:11PM
(#45408055)
Multi-threading does not imply shared ownership, it can also be achieved with message passing.