473,320 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 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
1 1680
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
4
by: Matthias Kaeppler | last post by:
Hi, I'm having a hard time figuring out how I can initialize a smart pointer based on a certain condition: if something then ptr = 0; // init with NULL else ptr = new XYZ; // init with a...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.