Smart Pointers

Discussion in 'C++' started by Sreeram, Dec 3, 2003.

  1. Sreeram

    Sreeram Guest

    Hello,

    I am having a doubt about smart pointer. I wrote a smart ptr class
    with limited functionalities. I want to do like this!

    CSmartPtr <CSomeClass> test = new CSomeClass;
    // here i need to call a function which will start a thread and
    returns once it starts the thread. Here i should able to increment the
    refcount of smart pointer and should decrement once the thread
    finishes executing. What i
    should do here? Can anybody please advice me?

    The code will looks like this.

    while (bDone) // loop forever
    {
    // do some process here.
    if (accepted)
    {
    CSmartPtr <CSomeClass> test = new CSomeClass;
    // call the thread starter function of the "CSomeClass".
    }
    }

    Thanks,
    Sreeram

    Note: Please send a copy of the answer to .
    Because i can acess this newsgroup only through google which will post
    the answer after 8 or 10hours. Thanks!
    Sreeram, Dec 3, 2003
    #1
    1. Advertising

  2. Sreeram

    tom_usenet Guest

    On 3 Dec 2003 05:12:20 -0800, (Sreeram)
    wrote:

    >Hello,
    >
    > I am having a doubt about smart pointer. I wrote a smart ptr class
    >with limited functionalities. I want to do like this!
    >
    > CSmartPtr <CSomeClass> test = new CSomeClass;


    Better would be:

    CSmartPtr<CSomeClass> test(new CSomeClass);

    The constructor should be explicit to prevent dangerous implicit
    conversions.

    > // here i need to call a function which will start a thread and
    >returns once it starts the thread. Here i should able to increment the
    >refcount of smart pointer and should decrement once the thread
    >finishes executing. What i
    >should do here? Can anybody please advice me?


    First, you need to ensure that your increment and decrement functions
    are atomic. Second, you need to block until the created thread has had
    a chance to create its own copy of the smart pointer. e.g.

    CSmartPtr<CSomeClass> test(new CSomeClass);
    startThread(&test);
    waitForThreadStarted();

    and the thread function should do:

    void threadFunc(void* p)
    {
    CSmartPtr<CSomeClass>* tempptr =
    reinterpret_cast<CSmartPtr<CSomeClass>*>(p);
    CSmartPtr<CSomeClass> ptr = *tempptr; //create local reference
    notifyLaunchThread();
    //carry on.

    //at exit, reference is released.
    }

    Details will vary depending on your threading library...

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    tom_usenet, Dec 3, 2003
    #2
    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. Evan
    Replies:
    1
    Views:
    1,308
    Howard Hinnant
    Jun 23, 2003
  2. Bonzo
    Replies:
    1
    Views:
    408
    Cy Edmunds
    Jul 23, 2003
  3. MotoK
    Replies:
    59
    Views:
    1,792
    Keith Thompson
    Sep 15, 2006
  4. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    463
    n2xssvv g02gfr12930
    Nov 27, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
Loading...

Share This Page