shared_ptr design question

Discussion in 'C++' started by g, Jan 6, 2007.

  1. g

    g Guest

    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,
    g, Jan 6, 2007
    #1
    1. Advertising

  2. g

    Pete Becker Guest

    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)
    Pete Becker, Jan 6, 2007
    #2
    1. Advertising

  3. g

    Daniel T. Guest

    "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.
    Daniel T., Jan 6, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    1,317
    Pete Becker
    Oct 23, 2005
  2. Colin Caughie
    Replies:
    1
    Views:
    691
    Shooting
    Aug 29, 2006
  3. zl2k
    Replies:
    4
    Views:
    600
  4. somenath
    Replies:
    6
    Views:
    99
    Stuart
    Feb 6, 2014
  5. somenath
    Replies:
    1
    Views:
    109
    James Kanze
    Feb 23, 2014
Loading...

Share This Page