Basic questions re threads and cleaning up

A

asfwa

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?
 
K

Kai-Uwe Bux

asfwa said:
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
 
J

Jim Langston

asfwa said:
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;
}
 
J

Joseph M. Newcomer

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.
****
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: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
 
J

Joseph M. Newcomer

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.
*****
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: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
 
K

Kai-Uwe Bux

Joseph said:
[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
 
W

werasm

asfwa said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top