shared_ptr design question

G

g

Hello,

Wouldn't be better shared_ptr constructor to have an argument
<true/false> for thread-safe reference counting?? The default should be
true and in case you dont want thread-safe RC create a shared_ptr with
false in constructor. The reason for this is to avoid the cost of the
memmory barriers where you don't need them.

At the moment if your application is multithreaded boost will choose to
use thread-safe RC but in many cases( local containers with
shared_ptrs) thread-safe RC doesn't have any sense.....becouse the
objects are owned by only one thread.

in this cases, the best way to go is to use an instrusive_ptr where the
developer implements the RC??

thanks in advance,
 
P

Pete Becker

g said:
Wouldn't be better shared_ptr constructor to have an argument
<true/false> for thread-safe reference counting?? The default should be
true and in case you dont want thread-safe RC create a shared_ptr with
false in constructor. The reason for this is to avoid the cost of the
memmory barriers where you don't need them.

At the moment if your application is multithreaded boost will choose to
use thread-safe RC but in many cases( local containers with
shared_ptrs) thread-safe RC doesn't have any sense.....becouse the
objects are owned by only one thread.

I think you're right that you shouldn't have to pay for sychronization
when you don't need it, I think a better approach is to have two
different types of shared pointer instead of one that may or may not be
synchronized. On the other hand, cases where you truly need to share
heap-allocated objects between threads are probably sufficiently rare
that maybe there's no need for synchronization at all. James Kanze, on
comp.std.c++, has suggested that auto_ptr is sufficient for sharing
objects between threads: create it in one thread and pass it to another.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
D

Daniel T.

g said:
Wouldn't be better shared_ptr constructor to have an argument
<true/false> for thread-safe reference counting?? The default should
be true and in case you dont want thread-safe RC create a shared_ptr
with false in constructor. The reason for this is to avoid the cost
of the memmory barriers where you don't need them.

I agree in general, but I don't think a constructor argument would be
the best choice. Better would be a templated proxy ala the shared
pointer class in Loki.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top