WG21-SG14/SG14

non thread-safe shared_ptr

onqtam opened this issue · 2 comments

Is there a chance to have a non thread-safe shared_ptr in the standard?

I was thinking either as a new template or maybe the current shared_ptr should have a template parameter if it's ref counting should be atomic.

There is quite a bit of interest in such a thing - see here

Andrei Alexandrescu talked at the cppcon how to implement such a pointer yourself but I feel the standard should provide one - see here

If facebook are doing it - it must be valuable.

Currently the only publicly available solution seems to be building boost with a flag/define to disable thread-safe reference counting.

It might be also good to have the optimizations that Andrei talks about - for the common case when the ref count only goes to 1 and then to 0.

I've already asked this in the google groups for the c++ standard/discussions and I was told that I should make a proposal but I'm not gonna go that route myself just yet.

jbcoe commented

I wonder if this could be done by implementers with the existing shared pointer?

In very simple cases the compiler can deduce that reference count need not be atomic.

I wonder if this could be done by implementers with the existing shared pointer?

AFAIK, libstdc++ switches to non-atomic implementation if compiled without -pthread flag.