Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > shared_ptr design question

Reply
Thread Tools

shared_ptr design question

 
 
g
Guest
Posts: n/a
 
      01-06-2007
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,

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      01-06-2007
g wrote:
>
> 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)
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      01-06-2007
"g" <> wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question related to stl vector and boost::shared_ptr zl2k C++ 4 10-22-2006 11:02 PM
#include <boost/shared_ptr.hpp> or #include "boost/shared_ptr.hpp"? Colin Caughie C++ 1 08-29-2006 02:19 PM
Newbie question about 'boost::shared_ptr' for.fun@laposte.net C++ 9 10-23-2005 02:20 PM
error with boost::shared_ptr<T> with incomplete T, VC7.1 ? Philippe Guglielmetti C++ 4 10-09-2003 05:30 PM
shared_ptr vs std::auto_ptr SerGioGio C++ 3 07-03-2003 02:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57