By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Smart Pointers

P: n/a
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 sr*********@netscape.net.
Because i can acess this newsgroup only through google which will post
the answer after 8 or 10hours. Thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 3 Dec 2003 05:12:20 -0800, sr*********@netscape.net (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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.