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? 7 1726
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
"asfwa" <sd****@hotmail .com> wrote in message
news:sM******** **********@news fe4-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;
}
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******@flound er.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
On Sun, 30 Oct 2005 16:37:53 -0800, "Jim Langston" <ta*******@rock etmail.com> wrote: "asfwa" <sd****@hotmail .com> wrote in message news:sM******* ***********@new sfe4-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******@flound er.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer wrote: there is no "threading library" in Windows
and my docs say that, for instance, CreateThread function is in
kernel32.lib
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
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_TE RMINATED; //Internal state - you can
ignore
DWORD exitCode;
::GetExitCodeTh read( handle, &exitCode );
if( exitCode == STILL_ACTIVE)
{
::CloseHandle( handle );
::TerminateThre ad( 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_p tr is a handy idiom.
delete pvalue; //Good
//Another version using scoped_ptr...
boost::scoped_p tr<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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: TomHorner |
last post by:
I have several quick questions about reorg's, and a request (favor).
1. The documentation says that reorg "Reorganizes an index or a table"
I cannot see how one would reorg ONE particular index - is there a way
to do this? More importantly, would you WANT to do this?
2. We plan on doing inplace reorg's due to the 24x7 nature of our
database servers. Are there any "undocumented" issues with these for
V8.1, FP2 or FP6?
|
by: Winbatch |
last post by:
Hi, I'm trying to learn multithreading and it doesn't seem to be working for
me. I have a feeling it has to do with the fact that I'm writing to files
rather than to printf, but maybe not. Basically, I wanted to see if it
would be faster to write to 4 files at the same time (parallel) rather than
4 in a row (serially). however, when my multithreaded code executes, it
seems to do them in order anyway (I expected to see Starting/Ending all...
|
by: Martin Maat |
last post by:
I am puzzled.
I have this object that uses a thread. The thread is encapsulated by the
object, the object has Start and Stop methods to enable the client to start
or stop the thread.
I found that the object will not be garbage collected while the thread is
running. Fair enough, the documented explanation is that the GC compresses
objects on the heap and needs to update references, the references will be
invalid for a couple of moments...
|
by: news.microsoft.com |
last post by:
I have a very multi-threaded Windows Forms application. There are many
BeginInvoke calls, as well as Thread/ThreadStarts.
My issue is that when I quit my application there are often threads hanging
around that prevent the process from dying.
I've gone through my code line by line looking for orphaned threads, but I'm
unable to figure out where they are being created.
Is there any way to kill all threads spawned by a process without...
|
by: Mr. Mountain |
last post by:
In the following code I simulate work being done on different threads by
sleeping a couple methods for about 40 ms. However, some of these methods
that should finish in about 40 -80 ms take as long as 2300 ms to complete.
This is fairly rare, but the test code below will definitely show it.
Somehow, I must not have my design right.
The idea of this code is that I do two different types of processing (
1-starting and 2-ending) based on...
| |
by: 1944USA |
last post by:
I am re-architecting a C# application written as a multithreaded
Windows Service and trying to squeeze every bit of performance out of
it.
1) Does the thread that an object is instantiated on have any impact on
its performnce?
Example: if I instantiate object "X" on thread "A" pass a reference
of "X" to Thread "B" and then have "B" run "X" (Exclusively). Does
|
by: Jordi Rico |
last post by:
Hi
What I want to do is for using with a PDA, but I think it's a VB.NET
related question, so I ask it here.
It's simple, an user is working with the pda, and from while to while,
has to send data to a server via webservice.
As the process to the webservice can take many seconds, and the user
has to continue working, I thought to send the process in a diferent
thread, and warn the user when it has finished. The problem is that the
user...
|
by: JJ |
last post by:
I've done a little multi-threading on winform apps some time ago now, so I'm
not a complete beginner, but best assume I am for any explanations..:
This is an asp.net 2.0 website, using c#:
I have a thread whose job is to send several emails (i.e. a long task that
mustn't be run by more that one user at a time).
It all works fine, but I am concerned about the lifetime of a thread in the
case of smtp errors, and how it will work with...
|
by: JackC |
last post by:
Hi,
I am trying to get posix threads working from within an object, heres
some code:
int NConnection::TheadControl()
{
int thread_id;
pthread_t new_connection;
pthread_create(&new_connection, NULL, PriC, (void *)thread_id);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |