Smart Pointers

S

Sreeram

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 (e-mail address removed).
Because i can acess this newsgroup only through google which will post
the answer after 8 or 10hours. Thanks!
 
T

tom_usenet

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
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top