[comp.os.os2.programmer] Recovering a Dead Thread's Stack, etc.

shiva@well.sf.ca.us (Kenneth Porter) (05/20/91)

How can I tell when a thread is dead and its resources
(especially its stack) can be reclaimed?  Presumably the thread
can't free its own stack since it needs it to call DosExit(). 
One can't tell that a thread is dead by testing for its ID
with, say, DosSuspendThread() (looking for the
ERROR_INVALID_THREADID) since another thread might be created
that would pick up a dying thread's ID.  Presumably
_beginthread() accomplishes the stack reclamation, since my
company has an application that spawns short-run threads left
and right with it and it doesn't lose memory over time.
 
Ken (shiva@well.sf.ca.us)

towfiq@FTP.COM (Mark Towfiq) (05/21/91)

>>>>> On 20 May 91 07:01:25 GMT, shiva@well.sf.ca.us (Kenneth Porter)
>>>>> said:

Ken> How can I tell when a thread is dead and its resources
Ken> (especially its stack) can be reclaimed?  Presumably the thread
Ken> can't free its own stack since it needs it to call DosExit(). 
Ken> One can't tell that a thread is dead by testing for its ID
Ken> with, say, DosSuspendThread() (looking for the
Ken> ERROR_INVALID_THREADID) since another thread might be created
Ken> that would pick up a dying thread's ID.  Presumably
Ken> _beginthread() accomplishes the stack reclamation, since my
Ken> company has an application that spawns short-run threads left
Ken> and right with it and it doesn't lose memory over time.

Well, you sort of answered the question yourself.  _beginthread() in C
6.0 does do the re-use/reclamation for you, since it can keep track of
which threads are used.  As far as a thread started by
DosCreateThread() goes, however (a thread which cannot call any C
run-time functions, by the way), the only reliable way I can think of
reclaiming such things is to have your thread set a flag or such upon
exit.
--
Mark Towfiq, FTP Software, Inc.                                  towfiq@FTP.COM
Work No.: +1 617 246 0900			      Home No.: +1 617 488 2818

  "The Earth is but One Country, and Mankind its Citizens" -- Baha'u'llah

larrys@watson.ibm.com (05/21/91)

In <24884@well.sf.ca.us>, shiva@well.sf.ca.us (Kenneth Porter) writes:
>
>How can I tell when a thread is dead and its resources
>(especially its stack) can be reclaimed?  Presumably the thread
>can't free its own stack since it needs it to call DosExit().
>One can't tell that a thread is dead by testing for its ID
>with, say, DosSuspendThread() (looking for the
>ERROR_INVALID_THREADID) since another thread might be created
>that would pick up a dying thread's ID.  Presumably
>_beginthread() accomplishes the stack reclamation, since my
>company has an application that spawns short-run threads left
>and right with it and it doesn't lose memory over time.
>
>Ken (shiva@well.sf.ca.us)

I don't know of a way to do this.  I had a rather large discussion with a
colleage yesterday about designing for multithreading.  I will be sending
a large posting with advantages/disadvantages of various multithreaded
programming techniques soon.

How do you know the application mentioned isn't losing memory?  Are you
using some sort of memory utility?

Cheers,
Larry Salomon, Jr. (aka 'Q')            LARRYS@YKTVMV.BITNET
OS/2 Applications and Tools             larrys@ibmman.watson.ibm.com
IBM T.J. Watson Research Center         larrys@eng.clemson.edu
Yorktown Heights, NY

Disclaimer:  The statements and/or opinions stated above are strictly my
own and do not reflect the views of my employer.  Additionally, I have a
reputation for being obnoxious, so don't take any personal attacks too
seriously.

oleg@watson.ibm.com (Oleg Vishnepolsky) (05/22/91)

In  <24884@well.sf.ca.us>  shiva@well.sf.ca.us (Kenneth Porter) writes:
>
> How can I tell when a thread is dead and its resources
> (especially its stack) can be reclaimed?  Presumably the thread
> can't free its own stack since it needs it to call DosExit().
> One can't tell that a thread is dead by testing for its ID
> with, say, DosSuspendThread() (looking for the
> ERROR_INVALID_THREADID) since another thread might be created
> that would pick up a dying thread's ID.  Presumably
> _beginthread() accomplishes the stack reclamation, since my
> company has an application that spawns short-run threads left
> and right with it and it doesn't lose memory over time.
>
> Ken (shiva@well.sf.ca.us)

Microsoft C compiler 6.0 frees application developers of necessity
to allocate their own stack. If you specify stack pointer as NULL,
then run-time library takes care of allocating the stack for you.
Then when the thread dies, the run-time deallocates the stack
for you. This way you bypass the whole problem of recovering the stack.

I am quoting from Microsoft C Advanced Programming Techniques:

"If you specify NULL for the stack address, _beginthread manages
allocation and deallocation of the thread stack for you. This
option is advantageous because it is difficult for your program
to determine when  a thread has terminated, so you cannot know
when to deallocate the thread stack".

Oleg Vishnepolsky