News for hackers. Stuff that matters.
Re:How to make Flappy Bird with C++ by another_another on Thursday August 25, 2022 @10:54AM (#32594462)
shared_ptr<> is most useful for multithreaded applications where a pointer can be safely shared amongst many threads, where the last one to go out of scope will call delete on the object.

To do this safely the shared_ptr<> class maintains a reference count which is protected by a mutex. This is pretty inefficient if you never share a pointer across threads.

Better to use unique_ptr<> where the Thing that created it is also the Thing that's responsible for deleting it - after making sure that anything it's shared the pointer with is already safely destroyed (another thread, a collection or some other object).

Re:How to make Flappy Bird with C++ by tlb on Thursday August 25, 2022 @11:05AM (#32594634)
std::shared_ptr doesn't use mutexes in any implementation I know of. They use atomic instructions for incrementing and decrementing the reference count, which are very fast.
Re:How to make Flappy Bird with C++ by intelVISA on Thursday August 25, 2022 @12:07PM (#32595734)
Plus their thread-safety is limited to the pointer's control block not the actual object it's pointing to.

e: nvm reading hard brain mutex too slow

Re:How to make Flappy Bird with C++ by another_another on Friday August 26, 2022 @04:25AM (#32604652)
You're right, an atomic inc/dec would also do just fine. I stand corrected.