Basic questions re threads and cleaning up

Discussion in 'C++' started by asfwa, Oct 30, 2005.

  1. asfwa

    asfwa Guest

    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?
     
    asfwa, Oct 30, 2005
    #1
    1. Advertising

  2. asfwa

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Oct 30, 2005
    #2
    1. Advertising

  3. asfwa

    Jim Langston Guest

    "asfwa" <> wrote in message
    news:sMa9f.11578$...
    > 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;
    }
     
    Jim Langston, Oct 31, 2005
    #3
  4. On Sun, 30 Oct 2005 17:05:05 -0500, Kai-Uwe Bux <> 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:
    Web: http://www.flounder.com
    MVP Tips: http://www.flounder.com/mvp_tips.htm
     
    Joseph M. Newcomer, Oct 31, 2005
    #4
  5. On Sun, 30 Oct 2005 16:37:53 -0800, "Jim Langston" <> wrote:

    >
    >"asfwa" <> wrote in message
    >news:sMa9f.11578$...
    >> 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:
    Web: http://www.flounder.com
    MVP Tips: http://www.flounder.com/mvp_tips.htm
     
    Joseph M. Newcomer, Oct 31, 2005
    #5
  6. asfwa

    Guest

    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, 2005
    #6
  7. asfwa

    Kai-Uwe Bux Guest

    Joseph M. Newcomer wrote:

    > On Sun, 30 Oct 2005 17:05:05 -0500, Kai-Uwe Bux <>
    > 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
     
    Kai-Uwe Bux, Oct 31, 2005
    #7
  8. asfwa

    werasm Guest

    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
     
    werasm, Nov 1, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sky Sigal
    Replies:
    13
    Views:
    872
    Sky Sigal
    Aug 3, 2004
  2. windandwaves
    Replies:
    27
    Views:
    732
    dorayme
    Feb 20, 2007
  3. Replies:
    1
    Views:
    280
    Richard Bos
    Apr 10, 2008
  4. Richard Maher
    Replies:
    38
    Views:
    1,118
    Larry K. Wollensham
    Apr 15, 2009
  5. Eric Wong
    Replies:
    0
    Views:
    96
    Eric Wong
    Mar 1, 2010
Loading...

Share This Page