Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a non-thread safe shared_ptr

Tags:

c++

stl

boost

I'm working on a mult-threaded program, but have a UI component that makes extensive use of std::shared_ptr to manage elements. I can guarantee that only one thread will ever use these shared_ptrs.

Is there a way to define a shared_ptr that doesn't incur the overhead of thread safe reference counting?

It could be based on boost::shared_ptr or std::shared_ptr.

EDIT: Thanks for answers mentioning intrusive_ptr. I neglected to mention that I also need weak_ptr functionality so that rules it out.

UPDATE: The answer for me is to use local_shared_ptr from Boost. See comment from 'he rambled'

like image 610
mpipe3 Avatar asked Jul 06 '11 08:07

mpipe3


1 Answers

Andrei Alexandrescu talked about implementing your own single threaded shared pointer class (with some additional optimizations) at the CppCon 2014

See the video here

And the slides here

I really think the standard or boost should supply a template parameter for using atomic ref counting in their shared ptrs though...

like image 92
onqtam Avatar answered Sep 23 '22 16:09

onqtam