Smart Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sreeram

    Smart Pointers

    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 sreeram0425@net scape.net.
    Because i can acess this newsgroup only through google which will post
    the answer after 8 or 10hours. Thanks!
  • tom_usenet

    #2
    Re: Smart Pointers

    On 3 Dec 2003 05:12:20 -0800, sreeram0425@net scape.net (Sreeram)
    wrote:
    [color=blue]
    >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;[/color]

    Better would be:

    CSmartPtr<CSome Class> test(new CSomeClass);

    The constructor should be explicit to prevent dangerous implicit
    conversions.
    [color=blue]
    > // 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?[/color]

    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<CSome Class> test(new CSomeClass);
    startThread(&te st);
    waitForThreadSt arted();

    and the thread function should do:

    void threadFunc(void * p)
    {
    CSmartPtr<CSome Class>* tempptr =
    reinterpret_cas t<CSmartPtr<CSo meClass>*>(p);
    CSmartPtr<CSome Class> ptr = *tempptr; //create local reference
    notifyLaunchThr ead();
    //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

    Comment

    Working...