KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q66295: What Happens to Stack Memory When Thread Terminates?

Article: Q66295
Product(s): See article
Version(s): 6.00
Operating System(s): OS/2
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 24-OCT-1990

Question:

If I call _beginthread() with NULL as the second parameter, the
run-time is supposed to create a thread stack for me. How is this
done? Also, when the thread terminates with _endthread(), is the
memory automatically released back to the operating system?

Response:

If _beginthread() is called with NULL for the thread stack, a stack of
size stack_size (passed as the third parameter) is malloc()'ed from
the heap. At the time the thread is terminated, the stack is still in
use; therefore, _endthread() does not automatically free it. In fact,
the last thing _endthread() does is push the appropriate arguments
onto the stack and call DosExit().

However, the memory is recovered. The next time that thread ID is
used, _beginthread() will check to see if the thread stack had been
previously malloc()'ed internally by the function. If so, it will call
free() at that time. Note that the thread memory doesn't go to the
operating system; instead, it is returned to the heap. Due to the fact
that OS/2 will use a thread ID from terminated threads first, there
will usually be only one thread stack not free()'ed.

As a side note, in 32-bit OS/2, this is all a moot point. The 32-bit
DosCreateThread() API call will automatically allocate/deallocate the
thread stack.

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.