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

Basic questions re threads and cleaning up

P: n/a
I'm new to C++ and I have some basic questions.

I have written an app that does some network stuff in a worker thread. The
thread function requests something from the server, gets it and creates an
object from the server response. Does this thread terminate/stop/die as soon
as it completes its last line of code? Or do I have to do something to kill
it?

I am used to coding Java so I am totally unsure of what I have to do to
"clean up" my variables/objects and free up memory etc. Should I use
"delete" for all objects I instantiate? How do I remove remove primitive
type variables?
Oct 30 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
asfwa wrote:
I'm new to C++ and I have some basic questions.

I have written an app that does some network stuff in a worker thread. The
thread function requests something from the server, gets it and creates an
object from the server response. Does this thread terminate/stop/die as
soon as it completes its last line of code? Or do I have to do something
to kill it?
The C++ standard does not mention threads at all (which makes it rather hard
to even argue about the correctness of threaded applications in C++).
Everything depends on the platform and threading library you are using. You
should take your question to a newsgroup with that scope. Here, this
question is off-topic.

I am used to coding Java so I am totally unsure of what I have to do to
"clean up" my variables/objects and free up memory etc. Should I use
"delete" for all objects I instantiate? How do I remove remove primitive
type variables?


As a rule of thumb, use delete on exactly those objects that you created by
means of new. You might consider using a shared_ptr<T> instead of raw
pointers, as those will take care of deleting objects automatically for as
long as you do not create dynamic data structures that have cycles.

On the other hand, objects that you create by means of a definition will be
destroyed when they go out of scope. This applies in particular to
variables of built in types.
Best

Kai-Uwe Bux
Oct 30 '05 #2

P: n/a

"asfwa" <sd****@hotmail.com> wrote in message
news:sM******************@newsfe4-gui.ntli.net...
I'm new to C++ and I have some basic questions.

I have written an app that does some network stuff in a worker thread. The
thread function requests something from the server, gets it and creates an
object from the server response. Does this thread terminate/stop/die as
soon as it completes its last line of code? Or do I have to do something
to kill it?
This is dependant on the implementation. C++ knows nothing about threads,
that's something the OS/compiler adds. Generally though, yes, the thread
terminates as soon as it completes the code. Check your OS documenation for
specifics.
I am used to coding Java so I am totally unsure of what I have to do to
"clean up" my variables/objects and free up memory etc. Should I use
"delete" for all objects I instantiate? How do I remove remove primitive
type variables?


For any object you use new on, you need to use delete on it. If you don't
use new, you don't need to use delete.

void somefunc()
{
int myint = 1; // No need to clear up memory
int* myint2 = new int(1); // Must call delete on the pointer

delete myint2;
}
Oct 31 '05 #3

P: n/a
On Sun, 30 Oct 2005 17:05:05 -0500, Kai-Uwe Bux <jk********@gmx.net> wrote:
asfwa wrote:
I'm new to C++ and I have some basic questions.

I have written an app that does some network stuff in a worker thread. The
thread function requests something from the server, gets it and creates an
object from the server response. Does this thread terminate/stop/die as
soon as it completes its last line of code? Or do I have to do something
to kill it?
The C++ standard does not mention threads at all (which makes it rather hard
to even argue about the correctness of threaded applications in C++).
Everything depends on the platform and threading library you are using. You
should take your question to a newsgroup with that scope. Here, this
question is off-topic.

****
There is clearly some confusion here. You are obviously confusing the kludges such as the
pthread library of Unix with true multithreading, and in any case, there is no "threading
library" in Windows (this concept should not be confused with the concept of the "thread
safe" libraries, which have nothing to do with implementing threading, only responding to
it), so the whole point of the above paragraph is completely irrelevant.

A thread executes in a stack. Stack semantics are well-defined by the standard.
****
I am used to coding Java so I am totally unsure of what I have to do to
"clean up" my variables/objects and free up memory etc. Should I use
"delete" for all objects I instantiate? How do I remove remove primitive
type variables?


As a rule of thumb, use delete on exactly those objects that you created by
means of new. You might consider using a shared_ptr<T> instead of raw
pointers, as those will take care of deleting objects automatically for as
long as you do not create dynamic data structures that have cycles.

On the other hand, objects that you create by means of a definition will be
destroyed when they go out of scope. This applies in particular to
variables of built in types.
Best

Kai-Uwe Bux

Joseph M. Newcomer [MVP]
email: ne******@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Oct 31 '05 #4

P: n/a
On Sun, 30 Oct 2005 16:37:53 -0800, "Jim Langston" <ta*******@rocketmail.com> wrote:

"asfwa" <sd****@hotmail.com> wrote in message
news:sM******************@newsfe4-gui.ntli.net...
I'm new to C++ and I have some basic questions.

I have written an app that does some network stuff in a worker thread. The
thread function requests something from the server, gets it and creates an
object from the server response. Does this thread terminate/stop/die as
soon as it completes its last line of code? Or do I have to do something
to kill it?
This is dependant on the implementation. C++ knows nothing about threads,
that's something the OS/compiler adds. Generally though, yes, the thread
terminates as soon as it completes the code. Check your OS documenation for
specifics.

****

Actually, the compiler doesn't add threading. The OS adds it.
*****
I am used to coding Java so I am totally unsure of what I have to do to
"clean up" my variables/objects and free up memory etc. Should I use
"delete" for all objects I instantiate? How do I remove remove primitive
type variables?


For any object you use new on, you need to use delete on it. If you don't
use new, you don't need to use delete.

void somefunc()
{
int myint = 1; // No need to clear up memory
int* myint2 = new int(1); // Must call delete on the pointer

delete myint2;
}

Joseph M. Newcomer [MVP]
email: ne******@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Oct 31 '05 #5

P: n/a

Joseph M. Newcomer wrote:
there is no "threading library" in Windows


and my docs say that, for instance, CreateThread function is in
kernel32.lib

Oct 31 '05 #6

P: n/a
Joseph M. Newcomer wrote:
On Sun, 30 Oct 2005 17:05:05 -0500, Kai-Uwe Bux <jk********@gmx.net>
wrote:
[snip]
The C++ standard does not mention threads at all (which makes it rather
hard to even argue about the correctness of threaded applications in C++).
Everything depends on the platform and threading library you are using.
You should take your question to a newsgroup with that scope. Here, this
question is off-topic.

****
There is clearly some confusion here. You are obviously confusing the
kludges such as the pthread library of Unix with true multithreading, and
in any case, there is no "threading library" in Windows (this concept
should not be confused with the concept of the "thread safe" libraries,
which have nothing to do with implementing threading, only responding to
it), so the whole point of the above paragraph is completely irrelevant.


Windows/Unix specific rants comparing different multithreading APIs are also
off-topic in this group; and so is any function or method you might use to
create or manipulate threads: no such function or method is mentioned in
the C++ standard.

A thread executes in a stack. Stack semantics are well-defined by the
standard. ****


The only stack that the C++ standard knows about is the container adaptor
std::stack. There is also the idiom "stack unwinding" that refers to
exception handling only. So, which clause of the standard exactly defines
"stack semantics"?

Instead of stack semantics, C++ actually defines a memory model and an
object model. Neither of them requires the existence of a stack.
Best

Kai-Uwe Bux

Oct 31 '05 #7

P: n/a

asfwa wrote:
I'm new to C++ and I have some basic questions.

I have written an app that does some network stuff in a worker thread. The
thread function requests something from the server, gets it and creates an
object from the server response. Does this thread terminate/stop/die as soon
as it completes its last line of code? Or do I have to do something to kill
it?
This depends on your OS and the API that you've used to create the
threads. I assume you are using windows. In that case, read the
documentation for the particular API calls you are making in the MSDN
archives, Platform SDK. As far as I remember, if a thread terminates
normally, you just have to call CloseHandle. This is the code that I
used within a wrapper class destructor - no guarantees - but seemed to
work fine (good idea to wrap your thread) - the comments are added for
your convenience:
Note:
All variables with trailing underscores are private data members of
this fictitious class:

//Destructor
MyTask::~MyTask()
{
if( taskHandle_ )
{
HANDLE handle = taskHandle_;
taskHandle_ = 0; //Invalidates taskHandle_ henceforth

threadState_ = eThreadState_TERMINATED; //Internal state - you can
ignore

DWORD exitCode;

::GetExitCodeThread( handle, &exitCode );
if( exitCode == STILL_ACTIVE)
{
::CloseHandle( handle );
::TerminateThread( handle, -1 );
}
else
{
::CloseHandle( handle );
}
}
}

I am used to coding Java so I am totally unsure of what I have to do to
"clean up" my variables/objects and free up memory etc. Should I use
"delete" for all objects I instantiate?
You are on dangerous ground. Coming from Java you have a lot to learn.
If you create objects on the heap (using operator new), you should
destroy them using operator delete. Automatic variables (variables
created on the stack) are destroyed automatically too (when the stack
unwinds). For these operator new is not used (in short - there is a
longer version). Example:

void foo()
{
int value = 0; //Will be automagically be destroyed at the end of foo
scope...
Object myObject; //... and this too.

int* pvalue = new int( value ); //calling copy-ctor for integer...,
//...needs to be destroyed explicitly at scope end or leak - for
this,
// boost::scoped_ptr is a handy idiom.
delete pvalue; //Good

//Another version using scoped_ptr...
boost::scoped_ptr<int> pvalue2( new int(value) );

//Because the scoped ptr is on the stack (or automatic), it will be
destroyed,
// and it's destructor will implicitly destroy (call delete on...)
// the wrapped ptr.
}
How do I remove primitive type variables?


See above. By primitive, I'm assuming you mean variables not created on

the heap.

Regards,

W

Nov 1 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.