A safe, non-owning C++ pointer class

(techblog.rosemanlabs.com)

21 points | by niekb 3 days ago ago

9 comments

  • mwkaufma 3 days ago

    > Note that our use case is in a single-threaded context. Hence, the word safe should not be interpreted as ‘thread-safe.’ Single-threadedness greatly simplifies the design; we need not reason about race conditions such as one where an object is simultaneously moved and accessed on different threads. Extending the design to a thread-safe one is left as an exercise to the reader.

    Why intentionally design a worse alternative to std::weak_ptr which has been around since C++11??

    • niekb 41 minutes ago

      (Author here.) That is a good question. For our use case, we in fact do not use std::shared_ptr in our implementation, but instead a single-threaded shared_ptr-like class that has no atomics (to avoid cross-core contention). However, when I wrote the blog-post, I replaced that not-so-well-known class by std::shared_ptr for the sake of accessibility of the blogpost for a general c++ audience, but by doing so, it indeed becomes a natural question to ask why one wouldn't use std::weak_ptr (which I hadn't realised when writing the post).

      One reason why this design can still be beneficial when using the standard std::shared_ptr in its implementation, is when you do not want to manage the pointee object by a std::shared_ptr (which is a requirement if you want to use std::weak_ptr). E.g., if you want to ensure that multiple objects of that type are laid out next to each other in memory, instead of scattered around the heap.

      Another goal of the post is to show this idea, namely to use a shared_ptr<T*> (instead of shared_ptr<T>), which is kind of non-standard, but can be (as I hope I convinced you) sometimes useful.

      • cherryteastain 21 minutes ago

        > but instead a single-threaded shared_ptr-like class that has no atomics (to avoid cross-core contention

        Why would there be contention in a single threaded program?

    • TinkersW an hour ago

      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..)

      • niekb 16 minutes ago

        > 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".

  • coneonthefloor an hour ago

    I’ve recently read the third edition of Bjarne’s “A tour of c++” (which is actually a good read). I feel the author of this post could benefit from doing so also.

  • abstractspoon 3 days ago

    > Extending the design to a thread-safe one is left as an exercise to the reader.

    Doesn't get much glibber than that!

    • niekb 34 minutes ago

      That was mostly meant as irony/a joke, but I admit that's not really clear from the text... For the sake of clarity, if you need thread-safety, probably best to just use std::shared_ptr / std::weak_ptr.

      • Kranar 20 minutes ago

        It's a common misconception that std::shared_ptr is thread safe. The counter is thread safe, but the actual shared_ptr itself can not be shared across multiple threads.

        There is now atomic_shared_ptr which is thread safe.